Re: Number Formatting
Re: Number Formatting
- Subject: Re: Number Formatting
- From: Daniel Todd Currie <email@hidden>
- Date: Sun, 20 Jun 2004 17:02:54 -0700
How are NSNumbers, NSDecimalNumbers, etc. even stored though? How is
the formatting of these numbers retained? If I'm going to subclass
NSDecimalNumber, should I be mucking around in all these @private
instance variables? Furthermore, where oh where is the NSNumber header
file? (It's not in Foundation.framework as expected.)
I guess a few tips on strategy here might be appreciated, since I also
have recently decided that I'm going to open my work on a whole slew of
math/number classes in an open-source framework. If any of you have
any interest in this project not sucking, please be compelled to chime
in. ;)
-- Daniel
On 2004 Jun 20, at 16:23, Jon Hull wrote:
I'm playing around with formatting numbers using +stringWithFormat:
and various format specifiers...
What I want is to be able to set a number to always be shown in
scientific notation, i.e.:
NSString *someString = [NSString stringWithFormat:@"%e", someNumber];
However, this places a strict precision on the number format, such
that if someNumber = 8000, someString will be @"8.0000000e+03". If I
use the %g specifier, it will create strings with a variable
precision, i.e. 8000000 = @"8e+06", but of course this is not always
in scientific notation. Aside from the argument for strict
significant digits, having all these extra zeros on %e and %f strings
is just plain ugly. How does %g provide such nicely formatted
numbers?
I obviously need to use some sort of NSNumberFormatter to get the
variable precision I desire, but I'm not seeing any methods in
NSNumberFormatter, NSNumber, NSDecimalNumber, etc. that will be at
all helpful. It seems like the only way to do what I want is to test
the string for unnecessary zeros in the NSFormatter, and then remove
them. How retarded... (Furthermore, I later plan to build in
support for engineering notation and significant digits, which will
no doubt spawn a host of new headaches.)
Unless someone here comes up with a better suggestion, I think it
will be time for me to make another framework that everyone can use
for this pretty basic functionality. Better suggestions?
It sounds to me like you want to subclass NSNumber or NSDecimalNumber
(maybe call it ScientificNumber) and add methods for sig-figs and any
other features you need. Then your NSFormatter subclass can use that
info to give you perfectly formatted strings (and create your
Scientific Numbers from strings). I know it sounds like more work...
but splitting it into two classes will make things cleaner (not to
mention much easier to update later).
If you have time, it sounds like this would make a useful IB Palette!
Thanks,
Jon
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.