• 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: Problem with NSDecimalNumber truncating zeros
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Problem with NSDecimalNumber truncating zeros


  • Subject: Re: Problem with NSDecimalNumber truncating zeros
  • From: WT <email@hidden>
  • Date: Tue, 7 Jul 2009 04:04:11 +0200

On Jul 6, 2009, at 10:35 PM, Eric Hermanson wrote:

Because of the functionality I am trying to achieve, I need to know the difference between 22 and 22.00, but I can't figure out how to get NSDecimalNumber to retain the trailing zeros. Does anyone have advice?

It looks like you need to extract the mantissas and exponents of NSDecimalNumbers that you didn't create yourself. There doesn't seem to be a direct way of doing that. I thought that - (NSDecimal)decimalValue might help, but the fields of the returned struct are private.


On Jul 6, 2009, at 10:52 PM, Fritz Anderson wrote:

NSDecimalNumber just keeps numerical values, not their history. If two numbers, _as numbers_, are equal, you should expect the normalized internal representations to be identical. 22.00 == 22 .

If you need to preserve context, you'll have to keep it yourself in another data structure (possibly one that wraps an NSDecimalNumber).

NSDecimalNumbers would be useless, if that were true. The whole point of having NSDecimalNumbers is to be able to preserve precision.


On Jul 7, 2009, at 2:14 AM, Eric Hermanson wrote:

this has absolutely nothing to do with NSNumberFormatter.

Not conceptually, but NSNumberFormatter may help in getting a solution to the problem. See below.


On Jul 7, 2009, at 2:14 AM, Eric Hermanson wrote:

NSDecimalNumber is losing information it had when I constructed it, which I do not want it to lose.

I don't think it is losing that information; it's just not making it available to you.


My suggestion, admittedly not optimal, is for you to get the strings for both numbers and parse them yourself to find out their mantissa lengths. That will differentiate between numbers that are numerically equal but of different precisions. In the specific example you gave, 22 and 22.00 are represented internally as

22:
mantissa = 22, exponent = 0

22.00:
mantissa = 2200, exponent = -2

Using NSNumberFormatter with a suitable formatting style, you get "22" and "22.00", which you can parse (ignoring + and - signs, commas, and periods) to find out that one has a precision of 2 digits while the other has a precision of 4 digits, which is what you're looking for.
_______________________________________________


Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Problem with NSDecimalNumber truncating zeros
      • From: Michael Ash <email@hidden>
    • Re: Problem with NSDecimalNumber truncating zeros
      • From: Kyle Sluder <email@hidden>
References: 
 >Problem with NSDecimalNumber truncating zeros (From: Eric Hermanson <email@hidden>)

  • Prev by Date: Cocoa System Requirements
  • Next by Date: Re: Cocoa System Requirements
  • Previous by thread: Re: Problem with NSDecimalNumber truncating zeros
  • Next by thread: Re: Problem with NSDecimalNumber truncating zeros
  • Index(es):
    • Date
    • Thread