• 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
Re: Accurate decimal numbers?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accurate decimal numbers?


  • Subject: Re: Accurate decimal numbers?
  • From: Pete Yandell <email@hidden>
  • Date: Sat, 1 Jan 2005 12:54:58 +1100

Rounding should work just fine for you:

double myValue;

if ([myScanner scanDouble:&myValue]) {
	myValue *= 1000; // Convert from metres to millimetres
	long myIntValue = lrint (myValue);
}

Let's say 1.55 scans as 1.549999. Multiply by 1000 to get 1549.999, and lrint will return 1550, which is the value you want.

As long as your floating point type has a little more precision than you need you shouldn't have any problems.

Pete Yandell


On 01/01/2005, at 11:08 AM, Mark Dawson wrote:

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


_______________________________________________ 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
References: 
 >Accurate decimal numbers? (From: Mark Dawson <email@hidden>)

  • Prev by Date: Re: Accurate decimal numbers?
  • Next by Date: Re: Accurate decimal numbers?
  • Previous by thread: Re: Accurate decimal numbers?
  • Next by thread: makeFirstResponder and Enter
  • Index(es):
    • Date
    • Thread