# Is there a way to round to 2 decimal places?

• 07-27-2007
osiris^
Is there a way to round to 2 decimal places?
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.
• 07-27-2007
laserlight
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.
• 07-27-2007
Salem
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.
• 07-27-2007
osiris^
Quote:

Originally Posted by Salem
You can do
printf("%.2f", myFloat );

Bingo! Nice one :)

Thanks guys.
• 07-27-2007
brewbuck
Quote:

Originally Posted by osiris^
Bingo! Nice one :)

Thanks guys.

Again though, using floats for monetary values is a bad idea.
• 07-27-2007
matsp
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 like
Code:

```  if (paid == invoiced) {     invoice.fully_paid = TRUE;   }```
will fail because the numbers don't match up.

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
• 07-27-2007
osiris^
Ah, that makes sense then. Thanks :)

On a side note, is it possible to have a &#163; 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.
• 07-27-2007
whiteflags
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.
• 07-27-2007
mike_g
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.

 Actually no need to curse ASCII:
Code:

```#include <stdio.h> int main() {     putchar(156);     getchar(); }```
[/edit]
• 07-27-2007
whiteflags
> putchar(156);

:rolleyes: 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.
• 07-27-2007
mike_g
Oh yeah, I guess that might cause problems.