0

(Firstly, I'm writing this program on a Mac, but it's in C++)

I'm writing a program that deals with float numbers; the float numbers will be holding different amounts of money. And the program will calculate a few percentages of the money, which will also be kept as float numbers..
I need to figure out how to track the decimal in these numbers; the reason is: I want the program to auto round the float numbers. This is because you don't list money as something like $97.843, you would want it to say $97.84.

So, how could I go about doing this?

James Litewski
  • 451
  • 2
  • 6
  • 16
  • 4
    I **highly** recommend reading [Why not use double or float to represent currency](http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) You simply do *not* want to do what you're doing. – Brian Roach Oct 26 '11 at 02:36
  • Don't use floating point for money. – Benjamin Lindley Oct 26 '11 at 02:36
  • Alright, what would you recommend I use for money? – James Litewski Oct 26 '11 at 02:38
  • The search feature? [Best way to store currency values in C](http://stackoverflow.com/questions/149033/best-way-to-store-currency-values-in-c) Hint: Keep track of cents, not dollars. – Brian Roach Oct 26 '11 at 02:42
  • @James: You use integers, where the number 1 represents not 1 dollar, but 1 cent, or some 10 based fraction of a cent. Or you write a class that does that internally, something like the CLR Decimal data type. – Benjamin Lindley Oct 26 '11 at 02:45
  • So, it would be like displaying data size in bytes, but then you convert it to mbs or gbs, ect before displaying it to the user? – James Litewski Oct 26 '11 at 02:48
  • So, I could have the user input a dollar amount, i.e. 97.84 then multiply it by 100? Then I'd just have to divide anything I want to display back to the user by 100. – James Litewski Oct 26 '11 at 02:53
  • Why do you say that your program "deals with float numbers". Is it homework assignment? What is the problem you are trying to solve: using fp or to represent money values? – curiousguy Oct 26 '11 at 03:40

1 Answers1

1

You never want to round your money values, except when displaying them to the user (or reporting). You don't want to store the rounded numbers obviously.

You can use C++ stream manipulators to change the default precision when outputting floating point values, e.g:

  std::cout.precision(n);
  std::cout << myFloat; 

See here for examples: http://www.fredosaurus.com/notes-cpp/io/omanipulators.html

fileoffset
  • 956
  • 6
  • 9
  • 2
    Dear god **no**. You *never want to use floating point* for anything that requires exactness. The answer is that you simply use a non-floating point type (int, long, etc) and keep track of *cents* – Brian Roach Oct 26 '11 at 02:44
  • 1
    That is correct, but the poster has not indicated whether that is a concern here. I am simply answering the question asked. – fileoffset Oct 26 '11 at 02:46
  • These are both very valid comments. – Zéychin Oct 26 '11 at 02:48
  • 1
    I'm not sure that telling someone the correct way to go about doing something wrong has merit. – Brian Roach Oct 26 '11 at 02:48
  • First of all, it is not 'the wrong way to do it'. You _can_ accurately use doubles for simple currency calculations, so long as you know what you are doing. Using a long double is better, and you need to use custom double comparison functions but it is not only possible, it's done in finance everywhere. – fileoffset Oct 26 '11 at 02:54