Re: rounding a float
Re: rounding a float
- Subject: Re: rounding a float
- From: Shawn Erickson <email@hidden>
- Date: Sun, 30 Nov 2003 11:34:18 -0800
On Nov 30, 2003, at 9:56 AM, Jay Rimalrick wrote:
I am attempting write a function that take a float (inFloat) and
returns a float
(hopefully rounded for currency)
Here is what I do inside my function:
NSString* myString = [[NSString alloc] init];
You allocate a NSString instance then...
myString = [NSString stringWithFormat: @"%f", inFloat];
...get a different instance created using convenience a method provided
by NSString. This new instances replaces the reference to the prior
instance you created, hence causing a leak of the prior instance.
NSDecimalNumber* myDecimalNumber = [[NSDecimalNumber alloc] init];
You allocate a NSDecimalNumber instance then...
myDecimalNumber = [NSDecimalNumber decimalNumberWithString:
myString];
...get a different instance created using convenience a method provided
by NSDecimalNumber. This new instances replaces the reference to the
prior instance you created, hence causing a leak of the prior instance.
I suggest you get a more solid understanding of objective-c object
concepts and memory management. Read over the following:
http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/
index.html
NSDecimalNumber* roundedDecimalNumber = [[NSDecimalNumber alloc]
init];
NSDecimalRound((NSDecimal*)roundedDecimalNumber,
(NSDecimal*)myDecimalNumber, 2, NSRoundPlain);
In the above you are casting a reference to an instance of
NSDecimalNumber to a pointer to an NSDecimal structure. This is wrong.
NSDecimalNumber is not a NSDecimal structure, you cannot do such a cast
(unless the docs state that you can). This is clobbering your
roundedDecimalNumber instance and resulting in the latter crash.
You should consider using decimalNumberByRoundingAccordingToBehavior:
instead.
If you want to get an NSDecimal from an NSDecimalNumber instance send
it the decimalValue message or create an NSDecimalNumber from an
NSDecimal use initWithDecimal:.
NSLog(@"%f", [roundedDecimalNumber doubleValue]);
return [roundedDecimalNumber doubleValue]; //i tried float value
too
-when I use [roundedDecimalNumber doubleValue] I get the error
"myProgram
has exited due to signal 11 (SIGSEGV)" in the debugger it tell me "GDB:
Program received signal: "EXC_BAD_ACCESS""
for some reason it is not passing back the roundedDecimalNumber
I suggest reading:
http://developer.apple.com/documentation/Cocoa/Conceptual/
NumbersandValues/index.html#//apple_ref/doc/uid/10000038i
So a possible way to do the above (written in Mail.app)...
Note if dealing with things like currency avoid using floats because of
base 10 precision issues, etc.. Instead try to get the values from the
user in string form or get the mantissa and exponent separately.
...
NSDecimalNumber* originalDecimalNumber =
[NSDecimalNumber decimalNumberWithString:myString];
// Likely keep a global copy of this behavior for reuse as needed...
NSDecimalNumberHandler* roundingBehavior =
[NSDecimalNumberHandler
decimalNumberHandlerWithRoundingMode:NSRoundPlain
scale:2
raiseOnExactness:YES/NO ...];
NSDecimalNumber* roundedDecimalNumber =
[originalDecimalNumber
decimalNumberByRoundingAccordingToBehavior:roundingBehavior];
...
return [roundedDecimalNumber doubleValue];
-Shawn
_______________________________________________
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.