View Full Version : C++ programming question
yawningdog
02-04-2002, 06:36 PM
I've got a function that returns a float because the program works with money. The problem is that the float returned is to the thousandth value, and since the variable represents a dollar value, I want to round to the nearest hundredth (penny). Anybody know how I can do that?
------------------
Give a man a fish, and you feed him for a day. Teach him to use the net, and he wont bother you for weeks.
d03boy
02-04-2002, 08:25 PM
Im not sure, but maybe try using a double instead of a float...
-d03boy
------------------
Alienware [Grey/Custom]
Intel Celeron 900MHz
128MB SDRAM
30GB Seagate HD
56x CD-ROM
8x HP CD-Writer
IntelliMouse [USB]
yawningdog
02-05-2002, 07:25 AM
Can't do it. I need to work with floats to get a decimal at all, unless there is a lot I don't undestand about doubles.
------------------
Give a man a fish, and you feed him for a day. Teach him to use the net, and he wont bother you for weeks.
Doubles can handle decimals as well, just larger ones. I can't see how it would help you any. Why don't you work with the more precise number while the program is running, and then output it in standard money format using stream manipulators found in iomanip.h? This will make the results more precise, and when output it will round automatically round the results.
Also, if you are working with money all of your input should not have more than two decimal places, so the output shouldn't have more decimal places than that.
Erik
By the way, a float holds up to 6 decimal places, not 3.
Gollum
02-07-2002, 12:33 PM
Hi yawning,
I'm trying to think back to my C days. When you call the number to be displayed did you use %.2f? Not sure if this is the same for C++, but will round the number to 2 decimal places. Hope this helps.
vBulletin v3.6.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.