Sunday, August 2, 2009

What's the most accurate way to compare money values represented as float variables in c++?

Using only standard c++ and stl. No outside functions or libraries.

What's the most accurate way to compare money values represented as float variables in c++?
The real question is whether you should be representing money as a float in the first place. Many, myself included, say no.





If you consider a banking application, for instance, even the smallest rounding differences when factored over billions of dollars per day for many days add up to very real amounts of money. Customers and regulators aren't too receptive to losing millions of dollars to "rounding errors".





Consider just using some form of int, long, or long long and using pennies (or whatever is the smallest unit of the currency you are dealing with) as your primary calculation unit. You add some minor computing overhead by having to multiply and/or divide by 100 at the appropriate times (e.g. displaying amounts) but your results will be vastly more accurate over time.
Reply:You can mimic rounding by adding 0.005 to the money value, then setting the precision to 2 decimal places. For example, $35.091 will become $35.096. Once the precision is set, C++ will truncate everything after 2 decimal points, and your money value will become $35.09. If, however, the money value was $35.097, adding 0.005 would make it into $35.102, and the truncated value will become $35.10. Just make sure you run whatever comparisons you are doing after the values are truncated.





Hope this helps,


Zach
Reply:You can always make use of sprintf().





sprintf() is like a printf() function. But rather than sending the output to the screen, it rather sent to a string (array of characters).. So, you make make use of the advantages of a printf() parameters and features.. What am I talking about?





Well, if you don't know how to present a floating number with its rounded form using printf(), here's how...





printf("%.2f", num);





This will print 'num' in two decimal places.. Notice the decimal point sign (.) and the number 2 following it (representing the number of decimal places)..





So, if you use the same procedure but in sprintf(), it would be like this...





char str[10];


sprintf(str, "%.2f", num);





where str is the array of characters to hold the converted value.. So later, if you want to use this value for computations, just convert the number using atof().. Like this..





num = atof(str);





Got it? Then, it would be easy for you later to compare money values... Though this is not really extremely fast for a solution, but it is actually just being resourceful. This is fast enough even for a huge application.. By the way, don't forget to include stdlib fo the atof() i think.. (^^,)





If you need to ask more, or if I missed to answer a part, just, leave a comment in one of my blog posts, then I'll know how to answer back.. Here's my blog.. http://ronaldborla.blogsome.com/


No comments:

Post a Comment