Rounding Floating Point Numbers In C++
Hi guys. I wondered something. I am starting to delve into the C++ language, and I have been writing several simple console applications to get myself familiar with the language. However, I have ran into a slight problem that I cant figure out how to fix.
I created a simple program that takes a dollar amount and calculates the sales tax and then displays the total cost of the item after tax. The program calculates everything fine, but for some reason if there is an amount with a zero in it, for example $181.20, then the program returns the value as $181.2 without the 0. I am also having trouble trying to get the program to round up or down. For example, sometimes it might display an amount as $145.678 rather than rounding it to $145.68.
I am declaring my variables as floats. Is this the correct type of variable to use? I know this should be an easy problem to fix but for some reason it has a beginner like me puzzled. I appreciate any help that I can receive on this matter.
Joshua a.k.a. CyberGeek
CyberGeek
If you are working with currency, you should really use an integer value and store the number of pennies (if you plan to deal with 10's of millions of dollars, maby use a long instead of an int).
To print the value, do something like:
int NumPennies = 234;
printf("The cost is: $%i.%i\n", NumPennies/100, NumPennies%100);
There are simple ways to manipulate a float to do what you want, but as they say when dealing with floats: "one is equal to two for significantly large quantities of one" ... google around if you want to learn more about floating point numbers and why I am steering you away from em for currency.
Hope that helps.
[edited by - Kevlar-X on January 10, 2003 8:57:36 PM]
To print the value, do something like:
int NumPennies = 234;
printf("The cost is: $%i.%i\n", NumPennies/100, NumPennies%100);
There are simple ways to manipulate a float to do what you want, but as they say when dealing with floats: "one is equal to two for significantly large quantities of one" ... google around if you want to learn more about floating point numbers and why I am steering you away from em for currency.
Hope that helps.
[edited by - Kevlar-X on January 10, 2003 8:57:36 PM]
"1 is equal to 2 for significantly large quantities of 1" - Anonymous
January 10, 2003 07:56 PM
If youre using printf as output.. i''d recommend doing it this way...
printf("%0.2f"; This will display only 2 decimal places.
printf("%0.2f"; This will display only 2 decimal places.
- Floats usually have seven digits
- Doubles usually have 15
- Long doubles usually have 19
So yes. You''ve definately selected the most approiate floating point type for your program.
Floating point data types have fixed precision
For instance- the type float you wish to use has the same number of digits on any given platform, no matter how large or small the number may be. Unfortunately, C++ does not have fixed point numbers that would give you control over the number of digits after the decimal point.
If your a beginner to C++, My suggestion is that until you learn the standard library functions, you define and call a function that displays the value as two integer quantities, and place a decimal point between.
Another alternative is to go ahead and use a float as you were and simple print the 0 value as a character literal.
Lots of answers - I''m surprised no one has bothered to mention the solution. Seriously, though: It may be a good idea to use integers, as Kevlar-X suggested, but there is a way of doing what you want to do in a ''C++ way'' (printf() is really more of a C solution): Stream manipulators. These change the format of input and input via I/O streams. For example:
#include <iostream>using namespace std; // I''m lazy ...int main(void){ float fSomeMoney = 250f; // Use a fixed precision (so it''ll print out the trailing 0''s // Use a precision of 2: Two digits after the decimal point cout << fixed << setprecision(2) << fSomeMoney << endl;}
First of all I want to thank all of you for your replies. You all gave me good ideas on how I could have done things differently.
Secondly, I have to say that you hit the nail right on the head Miserable. I looked up the setprecision manipulator and found some info on it. I had to include a preprocessor directive to the file and then I just added the line
cout.precision(5); to my function. It worked perfectly! I''m still not getting the zeros at the end of my decimal places, but the numbers are rounding up now. Thanks for setting me in the right direction. This programming stuff is fun! It makes you have to think, and I love solving problems so this is right up my alley! Thank you all again!!! I really appreciate the help!!!
Joshua a.k.a. CyberGeek
Secondly, I have to say that you hit the nail right on the head Miserable. I looked up the setprecision manipulator and found some info on it. I had to include a preprocessor directive to the file and then I just added the line
cout.precision(5); to my function. It worked perfectly! I''m still not getting the zeros at the end of my decimal places, but the numbers are rounding up now. Thanks for setting me in the right direction. This programming stuff is fun! It makes you have to think, and I love solving problems so this is right up my alley! Thank you all again!!! I really appreciate the help!!!
Joshua a.k.a. CyberGeek
CyberGeek
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement