• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Accurate decimal numbers?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Accurate decimal numbers?


  • Subject: Accurate decimal numbers?
  • From: Mark Dawson <email@hidden>
  • Date: Fri, 31 Dec 2004 16:08:54 -0800

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Accurate decimal numbers?
      • From: Pete Yandell <email@hidden>
    • Re: Accurate decimal numbers?
      • From: Brendan Younger <email@hidden>
  • Prev by Date: Re: Arrow keyEquivalent in InterfaceBuilder
  • Next by Date: makeFirstResponder and Enter
  • Previous by thread: Re: float & double scanner question
  • Next by thread: Re: Accurate decimal numbers?
  • Index(es):
    • Date
    • Thread