What would be the best way to remove the decimal point from a float number

Hi,

What would be the best way to remove the decimal point from a float number? For instance if I have a float number .2546 I would like to be able to remove the dot and use just the number (int) 2546 for some calculations.

Any idea how to do this?

Maybe, convert to string than remove the first character from the string than convert the string back to an int?

Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cmath>

int main()
{
    float values[] = { 123.45678901213, 123.45172901213, 123, 123.4, -123.45, 0 } ;
    std::cout << std::fixed ;
    std::cout.precision(8) ;

    for( float v : values )
    {
        float ival ;
        float fval = std::modf( v, &ival ) ;

        long before_decimalpt = std::lround(ival) ;

        // rounded to two digits after decimal point
        int after_decimalpt2 = std::lround( fval * 100 ) ;

        // rounded to four digits after decimal point
        int after_decimalpt4 = std::lround( fval * 10000 ) ;

        std::cout << v << ' ' << before_decimalpt << ' '
                   << after_decimalpt2 << ' ' << after_decimalpt4 << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/733b57965afccf68
That works, thanks a lot for your help!
Topic archived. No new replies allowed.