Converting a number

Hi everyone, this question might be a bit elementaty but I haven't done any programming in many years and I am a bit stuck.

I am trying to convert a number into a formatted number. I would like to do this.

example:
convert 450 into 4.5

If I do 450/100 I will get 4.50, I only want to get 4.5 without the zero on the end. Can someone show me an example on how this is done please.

Thanks
This is one way:

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

int main()
{
    for( unsigned int number : { 450, 451, 400, 12345, 12340, 12300, 1203, 0, 1 } )
    {
        const unsigned int a = number/100 ; // part before the decimal point

        unsigned int b = number%100 ; // remainder after div by 100

        std::string formatted_number = std::to_string(a) + '.' ;

        if( b== 0 ) formatted_number += '0' ; // a.0
        else if( b<10 ) formatted_number += '0' + std::to_string(b) ; // a.0b
        else
        {
            while( b>9 && b%10 == 0 ) b /= 10 ; // remove trailing zeroes
            formatted_number += std::to_string(b) ; // a.b
        }

        std::cout << number << " => " << formatted_number << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/29ecd3040c0be96d
That worked perfect, thank you very much.
Topic archived. No new replies allowed.