Rounding up (Was Re: Another newbie question...)
Rounding up (Was Re: Another newbie question...)
- Subject: Rounding up (Was Re: Another newbie question...)
- From: Esteban <email@hidden>
- Date: Mon, 28 Jan 2002 20:57:51 -0800
Kevin,
I can't remember if there is a rounding up function for any decimal
place in particular, but if you want to do this using Objective C, its
doable.
I believe what you want to look at is NSDecimalNumberHandler,
particularly at its
-initWithRoundingMode:scale:raiseOnExactness:raiseOnOverflow:raiseOnUnderflow:
raiseOnDivideByZero: initialization method, or even the convenience
constructor +decimalNumberHandlerWithRoundingMode:...
The value you pass in a scale seems to be the most important, since
depending on what this value is, rounding up happens. I pasted the
sample table for what happens depending on the Scale value, if the
original float/double value is 123.456
NSDecimalNumberHandler is listed under the documentation for Foundation
classes at
file:///System/Library/Frameworks/Foundation.framework/Versions/C/Resources/
English.lproj/Documentation/Reference/ObjC_classic/FoundationTOC.html
<File attached: Table.pdf>
As you can see a value of positive (+) 2 rounds to the hundreths place,
so you get 123.45
And a value of negative (-) 2 rounds to the hundreds place, so you get
100
If you prefer a nice C function that does this for you, Foundation also
provides an NSDecimalRound function, you can see the parameters for that
at
file:///System/Library/Frameworks/Foundation.framework/Versions/C/Resources/
English.lproj/Documentation/Reference/ObjC_classic/Functions/FoundationFunctions.
html
And if you would rather do this all in straight C and be elite (31337 or
leet as some say ;-) then you can do the following:
-------------------------
double myvalue = 123.456, result_value;
int temp_value;
temp_value = (int) (123.456 * 100); //multiplying by 100 gets us 12345.6
which we then cast as an int to get rid of the .6 fractional part.
//the casting to int of a double is very important in the line above
//temp_value should equal 12345
result_value = (double)(temp_value/100); //we dont really need to cast
back to a double, but the casting is there for illustration purposes.
//result_value should be equal 123.45, and we have rounded to hundredths
successfully.
-----------------------
Myself I prefer to use NSDecimalRound since its there, its probably a
lot easier to keep track of everything. Don't go about re-inventing the
wheel.
Hope that helped. Oh and I don't mean to be rude but if you'd looked at
the Foundation classes, you'd seen NSDecimalNumber and
NSDecimalNumberHandler. So its always good to look at the
documentation :)
-Esteban
On Monday, January 28, 2002, at 07:51 PM, email@hidden wrote:
>
I may only need one person to answer this, but if I have a float/double
>
and I want to round up the hundredths digit, can I do this with a
>
simple C function? I tried a rounding 100*number to the nearest ones
>
digit then returning back number/100, but it didn't work for some odd
>
reason. I still got a whole lot of decimals like 4.05000000030001 or
>
something like that...
>
>
I hope someone can help, and please forgive my ignorance :-)
>
-Kevin
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.