Problem with NSDecimalNumber truncating zeros
Example: NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithMantissa: 2200LL exponent:-2 isNegative:NO]; This results in a decimal number that is represented both internally, and as a string, as 22 instead of the desired 22.00 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? - Eric ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Problem with NSDecimalNumber truncating zeros
On 6 Jul 2009, at 3:35 PM, Eric Hermanson wrote: Example: NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithMantissa:2200LL exponent:-2 isNegative:NO]; This results in a decimal number that is represented both internally, and as a string, as 22 instead of the desired 22.00 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? 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). — F -- Fritz Anderson -- Xcode 3 Unleashed: Now in its second printing -- http://x3u.manoverboard.org/ ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Problem with NSDecimalNumber truncating zeros
On Jul 6, 2009, at 2:35 PM, Eric Hermanson wrote: NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithMantissa:2200LL exponent:-2 isNegative:NO]; This results in a decimal number that is represented both internally, and as a string, as 22 instead of the desired 22.00 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? You shouldn't make statements about the internal workings of API classes that you really don't understand. Specifically the statements about internal representation and truncating are false. That being said, if you want to compare the string representations of two numbers, then ensure that they are formatted similarly. Hence, use an NSNumberFormatter, preferably the same for both numbers. Keary Suska Esoteritech, Inc. Demystifying technology for your home or business ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Problem with NSDecimalNumber truncating zeros
On Jul 6, 2009, at 1: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, Perhaps -[NSDecimalNumberBehaviors scale] will help? -==- Jack Repenning jackrepenn...@tigris.org Project Owner SCPlugin http://scplugin.tigris.org Subversion for the rest of OS X ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Problem with NSDecimalNumber truncating zeros
Eric, I agree with Keary that NSNumberFormatter holds the solution to your problem. Here are links to pertinent documentation. HTH, Joel http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSFormatter_Class/Reference/Reference.html http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/DataFormatting/DataFormatting.html ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Problem with NSDecimalNumber truncating zeros
First of all, this has absolutely nothing to do with NSNumberFormatter. Second of all, the NSDecimalNumber is losing information it had when I constructed it, which I do not want it to lose. Instead of storing an exponent of -2 and a mantissa of 2200, it instead chooses to normalize the values to 0 and 22, respectively. While this does provide equal numbers as far as isEqual is concerned, I am forced to store my own context separately if I want the number to remember the scale it had when I constructed it. This is unfortunate. - Eric On Jul 6, 2009, at 6:17 PM, Keary Suska wrote: On Jul 6, 2009, at 2:35 PM, Eric Hermanson wrote: NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithMantissa:2200LL exponent:-2 isNegative:NO]; This results in a decimal number that is represented both internally, and as a string, as 22 instead of the desired 22.00 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? You shouldn't make statements about the internal workings of API classes that you really don't understand. Specifically the statements about internal representation and truncating are false. That being said, if you want to compare the string representations of two numbers, then ensure that they are formatted similarly. Hence, use an NSNumberFormatter, preferably the same for both numbers. Keary Suska Esoteritech, Inc. Demystifying technology for your home or business ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/zmonster%40mac.com This email sent to zmons...@mac.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Problem with NSDecimalNumber truncating zeros
Eric Hermanson wrote: Second of all, the NSDecimalNumber is losing information it had when I constructed it, which I do not want it to lose. Instead of storing an exponent of -2 and a mantissa of 2200, it instead chooses to normalize the values to 0 and 22, respectively. While this does provide equal numbers as far as isEqual is concerned, I am forced to store my own context separately if I want the number to remember the scale it had when I constructed it. This is unfortunate. If the NSDecimalNumber class doesn't meet your needs, consider using the C struct NSDecimal and the family of C functions found in NSDecimal.h. You should look carefully at the details of the struct first. Understanding the representation is crucial to knowing what is possible in distinguishing values from one another. http://developer.apple.com/documentation/Cocoa/Conceptual/ NumbersandValues/Articles/DecimalNumbers.html I found the above simply by starting at the NSDecimalNumber class reference and clicking the link to the Companion Guide, where the Using Decimal Numbers heading is visible. If NSDecimal still doesn't meet your needs, try these google keywords: bignum objective-c bignum bigdecimal Also see the list of libraries at the end of this article: http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic -- GG ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Problem with NSDecimalNumber truncating zeros
On Mon, Jul 6, 2009 at 8:14 PM, Eric Hermansonzmons...@mac.com wrote: First of all, this has absolutely nothing to do with NSNumberFormatter. Second of all, the NSDecimalNumber is losing information it had when I constructed it, which I do not want it to lose. Instead of storing an exponent of -2 and a mantissa of 2200, it instead chooses to normalize the values to 0 and 22, respectively. While this does provide equal numbers as far as isEqual is concerned, I am forced to store my own context separately if I want the number to remember the scale it had when I constructed it. This is unfortunate. Does the documentation guarantee that scale is preserved, or only value? If it does not guarantee that scale is preserved, then you shouldn't expect it to be. Classes are built for a purpose, and using them outside that purpose can mean they behave as designed instead of the way you want them to. Mike ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Problem with NSDecimalNumber truncating zeros
On Mon, Jul 6, 2009 at 7:04 PM, WTjrca...@gmail.com wrote: NSDecimalNumbers would be useless, if that were true. The whole point of having NSDecimalNumbers is to be able to preserve precision. Seems like you're confusing precision with sigfigs. NSDecimalNumbers exist because IEEE floating point doesn't represent a useful subset of the real numbers when doing base-10 arithmetic. They are not arbitrary-precision, sigfig-retaining numbers. --Kyle Sluder ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Problem with NSDecimalNumber truncating zeros
On Mon, Jul 6, 2009 at 10:04 PM, WTjrca...@gmail.com wrote: 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. According to the documentation, the whole point of having NSDecimalNumbers is to do base-10 arithmetic. I see nothing in there about preserving precision, neither in reference to the design intent nor in reference to the implementation. Perhaps I missed something? Times like this I wonder if I am the only one who consults documentation before posting Mike ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com