Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Accurate decimal numbers?



I have a couple of questions concerning how to use NSDecimal (or even if that's the correct way to go):

(1)
I'd like to convert decimal strings to unsigned longs; I'm not sure the best Cocoa way. I know that scanFloat, scanDouble doesn't work, because of conversion errors (i.e., "1.55"-->1.54999). In my case, its very important to convert the string accurately, as I'd be converting something like 1.555m --> 1555mm.


It looks like NSDecimal (scanDecimal) is the way to go, but I'm not sure the best way to use it. I know I can use NSDecimalMultiplyByPowerOf10 to convert the NSDecimal returned from scanDecimal. What's the best way to convert an NSDecimal to an unsigned long? I wasn't sure if I needed to create an NSDecimalNumber, then do a [num double] to do a forced conversion.

(2)
Also, what's the best way to determine whether a number is a whole number? (i.e. the fractional portion is 0-->1.0). I've usually done something along the lines of


if (x != (float)((int)x))

where x is a float or double; is there an easier way to do this using an NSDecimal number?

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.