Hi all,
I've got a program which returns various currencies to the user, so I need these to be rounded to two decimal places. I've searched the boards and everyone just talks about ceil() and floor(), which aren't really enough.
Thanks.
Hi all,
I've got a program which returns various currencies to the user, so I need these to be rounded to two decimal places. I've searched the boards and everyone just talks about ceil() and floor(), which aren't really enough.
Thanks.
You could multiply by 100, do the rounding, then divide back.
Incidentally, using floating point for money may result in undesirable inaccuracy. It might be better to use integers, or some custom decimal type instead.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
You can do
printf("%.2f", myFloat );
Or multiply by 100 and take the integer part of the result. Then use /100 and %100 to extract the parts you want.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
The reason you shouldn't use floating point for money-counting is that the result of a floating point calculation isn't PRECISE. Pariticularly certain numbers can not be described exactly in floating point, for example 0.7. So if you ever take 70% of something, it will be 69.999999999...% of it. (or something is 70 pence/cents/öre or whatever the small part of the currency is called)
The real problem here is that if you enter a new value, say someone pays in a check for an invoice, you enter 123.71 as the amount, and you compare 123.71 with the value you calculated (which PRINTED as 123.71, but the actual value is 123.7091214823), it will not be precisely that. So a comper likewill fail because the numbers don't match up.Code:if (paid == invoiced) { invoice.fully_paid = TRUE; }
As other said, count the money in a LONG integer and use it to indicate the smaller part of the currency (pence, cents or whatever), and then just make a special print routine to print the value as (v / 100) . (v % 100). If you deal with really large amounts, you may want to use a 64 bit integer.
Of course, in C++ you could implement a "monetary" class, which does this automagically.
--
Mats
Ah, that makes sense then. Thanks
On a side note, is it possible to have a £ sign in C? I'm presuming you have to use some unicode value, because when it was printed to the screen, it came out as a "u" with an accent on it.
Your options include but are probably not limited to: (from easier to harder)
1. Use pounds, but suffix the amount with GBP, and curse ASCII.
2. Convert to dollars, use dollars, and curse ASCII.
3. Attempt to use Unicode or multibyte strings, and curse ASCII.
In college, using Borland, I could get pound symbols by holding down alt and typing 0162 (or something like that). Dosent seem to work at home in DevC++ tho. So I guess I just use GBP and curse ASCII.
[Edit] Actually no need to curse ASCII:
[/edit]Code:#include <stdio.h> int main() { putchar(156); getchar(); }
Last edited by mike_g; 07-27-2007 at 12:33 PM.
> putchar(156);
That's from the extended set, and there are a couple, so stick to the standard one. Vanilla char might not be the same as unsigned char either, which might lead to wrong output as well.
Oh yeah, I guess that might cause problems.