I would like to take a decimal or non-decimal value and store it as a string with exactly 2 decimal places in C . I want to do this to show it as a monetary value, so it is always $10.50 or $10.00 rather than $10.5 or $10.
I don't just want to print this, I want to store it, so I don't believe setprecision will work here. I'm doing this in a Qt application, so if there is a way to do it using Qt I can use that as well.
For example:
int cents = 1000;
std::string dollars; //should get value from cents formatted to 10.00
UPDATE: It seems I don't have the vocabulary yet as I am just beginning to learn C to articulate what I am trying to do. Here is what I want to do using Python:
str_money = '$ {:.2f}'.format(num)
In this example, num can be a decimal or not (10 or 10.5 for example) and str_money is a variable that is assigned the value of num as a decimal with exactly 2 numbers after the decimal (in this example, str_money would become 10.00 or 10.50). I want it to store this in a string variable, and I don't need it to store the '$' with the value.
Can I do this in C ?
CodePudding user response:
If you want to store a fixed number of decimal places, a float is not what you want. You want a fixed-point number. With money, the basic idea is to store the value as "cents" in an integer. Then you can perform a division by 100 whenever you want to output the value as "dollars". (Or have a custom output function or operator that formats the output correctly.)
One of the big benefits to fixed-point arithmetic is that you can avoid rounding errors. Floating point numbers are really bad at exactly storing decimal fractions, so dealing with "tenths" or "hundredths" can easily result in rounding errors that can add up in long running, or complicated programs.
How you implement your fixed point numbers is largely up to you. You might find a library that has a fixed-point class, you could implement your own, or you could just manipulate integer variables.
CodePudding user response:
If you want this happen on output then you can use setprecision () method as it sets the decimal precision to be used to format floating-point values on output operations.
and check this solution for the problem
https://www.geeksforgeeks.org/rounding-floating-point-number-two-decimal-places-c-c/
