numbers after the floating point

if we have a decimal number like c=3.46

And i want to set two number, a and b

now a= static_type<int>(c); so a=3;
and i want b= 46

which is the two numbers after the decimal
how can I do that ? how can I set b = 46 ?

P.S: i do not know what c equals to. now it's two number after the floating point but it might be more or less

So are you saying you know a and b but not c? This you would mean you would have to divide b by 10digits and add it to a.
giblit
i do not know anything. c is being set by the user.
You have 3.46 and the integer part already (3) - so the difference between those is the fractional amount. But you want to get rid of the decimal point?
If it is set by the user you will know what it is after they give it a value. This would mean that you will want to find a a = static_cast<int>(c); then subtract a from c and multiply by 10 until there are no more decimal digits.

You could also simply use stringstreams.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <sstream>
#include <string>

int main()
{
    double input;
    std::cout << "Please enter a floating-point number: ";
    std::cin >> input;
    
    std::stringstream ss;
    ss << input;
    int characteristic, mantissa;
    char decimal;
    ss >> characteristic >> decimal >> mantissa;
    
    std::cout << "Characteristic: " << characteristic << std::endl;
    std::cout << "Mantissa: " << mantissa << std::endl;
    
    return 0;
}
Please enter a floating-point number: 12.414
Characteristic: 12
Mantissa: 414

http://coliru.stacked-crooked.com/a/18e61d93a916d8c8

Though keep in mind if you have a large floating-point number you are going to want to include iomanip and then use std::setprecision

How do you multiply by 10 until there is no more decimal point ? we don't know how many times we have to multiply

let me make my question simpler with this program

1
2
3
4
5
6
7
8
9
cout << "enter length of the road, and enter how fast you are going " << endl;
cin >> length >> mph;

time = length/mph;
hours = static_cast<int>(time);
minutes = (the numbers after the decimal point) * 60;
seconds = (the numbers after the decimal point of minutes) * 60



so how do we get those numbers after the decimal point ? I know it involves modulus
Last edited on
Getting the mantissa would be very hard with modulus.

How do you multiply by 10 until there is no more decimal point ?
If you keep multiplying by 10 eventually the mantissa will be 0. Basically something like:

a == 0.12345
0.12345 - 0 = 0.12345, the mantissa is not 0

a * 10 == 1.2345
1.2345 - 1 = 0.2345, the mantissa is not 0

a * 100 == 12.345
12.345 - 12 = 0.345, the mantissa is not 0

a * 1000 == 123.45
123.45 - 123 = 0.45, the mantissa is not 0

a * 10000 == 1234.5
1234.5 - 1234 = 0.5, the mantissa is not 0

a * 100000 = 12345.0
12345 - 12345 = 0.0, the mantissa is 0

Keep in mind that checking if the mantissa is equal to 0 you will need to use a very small threshold something like double threshold = 0.00000000000001;
Last edited on
got the logic thanks
Topic archived. No new replies allowed.