How do I move a decimal?

Hey guys

Am trying to move a decimal towards the left side but to be am finding it so difficult. I know am supposed to do a multiplication or even a division but actually I really don’t want to do that thing at all. Anybody of you who has another way that I should use to handle this thing? Am interested in making my program is such a way that when the user keys in a number like 0.34 in decimal, the outcome is in percentage, let us say 34%.https://www.theengineeringprojects.com/2019/11/introduction-to-structures-in-c.html
Last edited on
Just multiply by 100
this is a case of 'yes, you can do that' but 'you should not'. The fastest, bestest way here is to multiply by a power of 10.
you 'can' read the input as a string, parse the string, extract the digits, and format the output.
here, for example, you can do a find() and a substr() on a string to find the '.' and then take everything after it (substr(find('.')+1) would turn "0.34" into "34". Then if you actually NEED to use the number as a value, you have to add another step to convert string to number. All that is way too much hassle most of the time compared to input*100, and ... if the user puts in .345, you get 345, not 34.5. You have to add yet one more step to put the decimal back in there after 2 digits, which you can do, but I will leave that to you to study the sting library for the best way.
Last edited on
Am trying to move a decimal towards the left side

Is not the same as

a number like 0.34 in decimal, the outcome is in percentage, let us say 34%

Moving a decimal point to the LEFT makes the number smaller, division by a power of 10 is usually done. 0.34 becomes 0.034 and so on.

To the RIGHT, to turn a number into a percentage, is done by multiplying with a power of 10, as the others have said.
:P 1/1000 is a power of 10 ... a negative one
Last edited on
Multiplying by a negative power of 10 is essentially division, and dividing by a negative power is multiplication....

> I know am supposed to do a multiplication or even a division but actually I really don’t want to do that thing at all.
> Anybody of you who has another way that I should use to handle this thing?

Something like this, for example, to move the decimal point to the right by one.

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
27
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>

// shift the decimal point to the right by one
// invariant: digits form a valid decimal number
std::string mul_10( std::string digits )
{
    digits += '0' ;
    if( auto pos = digits.find('.') ; pos != std::string::npos )
        std::swap( digits[pos], digits[pos+1] ) ;
    return digits ;
}

int main ()
{
    const double num = 1234567890123456789.012345 ;
    std::cout << std::fixed
              << "   num == " << std::setw(30) << num << '\n'
              << "num*10 == " << std::setw(30) << num*10 << "\n\n" ;


    const std::string str_num = "1234567890123456789.012345" ;
    std::cout << "        str_num == " << std::setw(30) << str_num << '\n'
              << "mul_10(str_num) == " << std::setw(30) << mul_10(str_num) << "\n\n" ;
}

http://coliru.stacked-crooked.com/a/e110d20f6ce39017

Possible output:
   num ==     1234567890123456768.000000
num*10 ==    12345678901234567168.000000

        str_num ==     1234567890123456789.012345
mul_10(str_num) ==    12345678901234567890.123450


Note: there may be a case for doing this without numeric multiplication/division when an arbitrary precision number type is not used.
Multiplication is repeated addition, to move a decimal point two digits to the right you do addition 100 times:

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

int main()
{
   double num { 0.34 };

   int exponent { 2 }; // i.e., 10 raised to the 2nd power (100)

   double raised { 0.0 };

   for (unsigned i { 0 }; i < std::pow(10, exponent); ++i)
   {
      raised += num;
   }

   std::cout << std::fixed << std::setprecision(3) << num << ", " << raised << '\n';
}

0.340, 34.000
You can use basic numeric operations to change the decimal values of your output.
Topic archived. No new replies allowed.