# rounding to nearest penny

• 09-05-2001
sizzle_chest
rounding to nearest penny
I am a newb. this code works but i need to round to the nearest penny and can't find the right function. I found floor() and ciel() but they only round up or down to the nearest integer. Any hints or ideas.
• 09-05-2001
Stoned_Coder
I haven't looked at your code but one way to do what you want is to multiply by 100 to a whole integer then apply floor() or ciel() and then divide by 100 to return to the decimal.
• 09-05-2001
Unregistered
i tried floor() and ciel() but they only round up or down. i want to round to nearest penny(>= 5 round up, < 5 round down). Tried to do:

//total is a floating value

total = total * 100;
total = floor(total);
total = total/100;

it work but is not acurate to the penny, it always rounds down.

• 09-05-2001
Stoned_Coder
well now all you have to do is use floor when you have to round down and ceil when you have to round up. You can check the third decimal place and if its 5 or greater round up or otherwise round down. One possible way of doing this is to use sprintf to format your number as a string in memory.search for the decimal point and then check the third char after that to decide whether to round up or down. There are probably easier ways of doing it too.
• 09-06-2001
Witch_King
last = number % 10; should tell you what the last number is. I think, anyway.
• 09-06-2001
Stoned_Coder
:o I was soooo mashed last night... :o

here's how to do it properly

multiply by 100