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.
This is a discussion on rounding to nearest penny within the C++ Programming forums, part of the General Programming Boards category; I am a newb. this code works but i need to round to the nearest penny and can't find the ...
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.
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.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
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.
im not asking for the answer exactly but point me in the right direction, please.
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.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
last = number % 10; should tell you what the last number is. I think, anyway.
I compile code with:
Visual Studio.NET beta2
I was soooo mashed last night...
here's how to do it properly
multiply by 100
add 0.5
floor()
divide by 100.
that will round to the nearest penny....
sorry about the stoned ramblings.....
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
that does the trick. i was missing the add .5.
thanx