Think about it, you should try to avoid converting NSDecimalNumber or NSDecimals to and from int, float, and double values ββfor the same reasons that NSDecimalNumbers is recommended: loss of precision and binary floating point problems. I know sometimes this is inevitable (taking input from a slider, doing trigonometric calculations, etc.), but you should try to take data from users as NSStrings, and then use initWithString: locale: or decimalNumberWithString: locale: to generate NSDecimalNumbers. Do all your math with NSDecimalNumbers and return your view to users or save them in SQLite (or everywhere) as their string description using descriptionWithLocale :.
If you need to enter data from int, float or double, you can do something like the following:
int myInt = 3; NSDecimalNumber *newDecimal = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%d", myInt]];
or you could follow Ashley's suggestion to make sure you are safe in decimal.
Brad Larson Jan 19 '09 at 16:20 2009-01-19 16:20
source share