Formatting a set of NSDecimalNumber values
Formatting a set of NSDecimalNumber values
- Subject: Formatting a set of NSDecimalNumber values
- From: Sixten Otto <email@hidden>
- Date: Fri, 10 Aug 2012 08:58:05 -0700
I'm working on an app where I need to take several sets of numeric
values (currently stored as NSDecimalNumber), and display them in
columns. Each set will have approximately the same magnitude, but the
magnitude of each set may change, and I won't know ahead of time what
they are. So one might be values like (1.23, 2.3, 1.567), and another
(1234.1, 2345, 1999.4).
In order to make the columns line up, and to know how much space I'll
need, what I'd like to do is to figure out, for each set of numbers,
the width of the format I'll need. In other words, the maximum number
of integer places, and the maximum number of fractional places. For
the examples above: 1 and 3 in the first case, 4 and 1 in the second.
Now, the NSDecimal representation would answer this directly, by
combining the length of the mantissa and the value of the exponent.
The problem is, the fields of that structure are explicitly private,
and there doesn't appear to be any API to get the values of the
mantissa and exponent out of it.
One workaround might be to convert each number to a string in the
POSIX locale, and then measure its length and the position of the
decimal point. But that seems like a very long way around. And I'm
shamefully sketchy on my C math, but I'd guess that there's a route to
the answer there, also (possibly with some loss of precision?).
What's the simplest and/or most efficient way of deriving this information?
_______________________________________________
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