how to move a decimal

Oct 4, 2021 at 3:28pm
I'm having a little bit of a hard time figuring out how to move a decimal to the left. I understand I could divide or multiply, but that is not what I want to do. Is there another method? I want the user to type in a number like 0.23 and have it be read 23%
Oct 4, 2021 at 3:52pm
A number is a number as far as how it's stored. Do you really mean that you want .23 displayed as 23% ?

1
2
3
4
5
6
7
#include <iostream>

int main() {
	const double no {0.23};

	std::cout << no * 100 << "%\n";
}



23%

Oct 4, 2021 at 4:01pm
Yes so when the user types in 0.23 it will display 23%. Is there a way without dividing or multiplying? Like moving the decimal? I meant to put move it to the right earier.
Oct 4, 2021 at 4:14pm
I understand I could divide or multiply, but that is not what I want to do.
This would certainly be the most natural way to handle this. Why are you restricting yourself?

Like moving the decimal? I meant to put move it to the right earier.
The decimal could be moved. This would be string manipulation. But your example isn't just moving the decimal; you're removing it.

Aside from doing arithmetic, the only other way I can think of is string manipulation. But what exactly are the requirements here? Should "0.123" be "12.3%" or "12%"? Should some sort of rounding happen for 0.127? Do you care about handling invalid input? What if the user enters "00000.23"? "000001.23"? "-0.23"? Should "0.230" be a different output than "0.23"?
Last edited on Oct 4, 2021 at 4:17pm
Oct 4, 2021 at 4:29pm
you can certainly move the . around in a stringified version of the number.
this is more expensive (CPU terms) and more convoluted (code terms) ... its doable but questionable. Multiply of doubles is really fast, and display is really slow, so you can't really improve the performance or anything... the act of converting it to text dwarfs the multiply.
Oct 4, 2021 at 4:38pm
Small example program (with no real error checking, and yes there are some funny edge cases you can find. It's a start to help you out):
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
    std::string number;
    std::cin >> number;
    
    size_t index = number.find('.');

    // padding with zeroes to make string manipulation easier
    number.resize(number.find_last_not_of('0') + 1);
    if (index != std::string::npos)
    {
        if (number.substr(index).length() <= 3)
        {
            number = number + std::string(3 - number.substr(index).length(), '0');
        }
    }
    else
    {
        number = number + ".00";
    }

    index = number.find('.');

    // remove decimal point
    number.erase(std::remove(number.begin(), number.end(), '.'), number.end());

    // add it back in
    if (index + 2 != number.length())
    {
        number.insert(index + 2, 1, '.');
    }
  
    // remove leading zeroes (https://stackoverflow.com/a/31226728)
    number.erase(0, std::min(number.find_first_not_of('0'), number.size()-1));
    if (number[0] == '.')
    {
        number = "0" + number;   
    }

    std::cout << number << "%\n";
}


I'm sure I made this more convoluted than necessary. Does not handle negatives.

00123.0032100
12300.321%
Last edited on Oct 4, 2021 at 5:37pm
Oct 4, 2021 at 8:25pm
Of course if it’s multiplication that’s precluded then just add 0.23 a 100 times. It’s a simple copy and paste exercise on number + number ... and put a % at the end of the cout
Oct 4, 2021 at 8:45pm
so many math tricks you can pull. exp(log10(x)+2).

or derpy string processing 101: print 1 char at a time, when you find a '.' skip it, count to 2, write it, keep printing chars.
Last edited on Oct 4, 2021 at 8:47pm
Oct 4, 2021 at 9:18pm
That latter method would print "23.%"
Last edited on Oct 4, 2021 at 9:19pm
Oct 4, 2021 at 9:53pm
yea, its derpy for a reason. that is valid, but maybe not desired. who knows?
Oct 4, 2021 at 9:55pm
The purpose of homeworks like this is to familiarize yourself with output manipulators.

To get a fixed precision decimal, use

std::fixed
std::precision()
std::setfill()

And, as always, std::setw() is useful.

If you need to multiply or divide to get the number to look like you want, just do it.

Hope this helps.
Oct 5, 2021 at 8:03am
PhoenixS2020 wrote:
I understand I could divide or multiply, but that is not what I want to do.

Why not?

What ... at the end of the day ... are you actually trying to do?

A percentage is just the number of hundredths, so simple arithmetic with fractions says multiply by 100. Input/output will far outweigh any time spent on a single multiplication operation. You can format the output however you like once you've done the multiply.

Please don't persist with an XY problem.
Last edited on Oct 5, 2021 at 8:03am
Topic archived. No new replies allowed.