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

Number Formatting


  • Subject: Number Formatting
  • From: Daniel Todd Currie <email@hidden>
  • Date: Sun, 20 Jun 2004 14:41:08 -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?

-- Daniel Todd Currie
_______________________________________________
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: Jon Hull <email@hidden>
  • Prev by Date: Re: sortedArrayUsingFunction:context: why context is of type void?
  • Next by Date: Re: POS:0: pbxcp: Info.plist: No such file or directory SEMI-SOLVED
  • Previous by thread: POS:0: pbxcp: Info.plist: No such file or directory
  • Next by thread: Re: Number Formatting
  • Index(es):
    • Date
    • Thread