• 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: Number Formatting
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Number Formatting


  • Subject: Re: Number Formatting
  • From: Jon Hull <email@hidden>
  • Date: Sun, 20 Jun 2004 16:23:46 -0700

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.


  • Follow-Ups:
    • Re: Number Formatting
      • From: Daniel Todd Currie <email@hidden>
References: 
 >Number Formatting (From: Daniel Todd Currie <email@hidden>)

  • Prev by Date: Re: POS:0: pbxcp: Info.plist: No such file or directory SEMI-SOLVED
  • Next by Date: Re: Number Formatting
  • Previous by thread: Number Formatting
  • Next by thread: Re: Number Formatting
  • Index(es):
    • Date
    • Thread