• 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: Formatting numbers, currency, and dates based on locale
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Formatting numbers, currency, and dates based on locale


  • Subject: Re: Formatting numbers, currency, and dates based on locale
  • From: Charles Srstka <email@hidden>
  • Date: Sun, 5 May 2002 18:25:35 -0500

NSNumberFormatter is what you want for this. It has a method called setLocalizesFormat: which, if you pass YES to it, causes the formatter to localize things like thousands separators, decimal points, etc.

Unfortunately, NSNumberFormatter has a bug where it doesn't recognize that the setLocalizesFormat: property is set to YES except for when it is unarchived, as from a nib file. This means that it works great if you attach it to an interface element in Interface Builder, but if you call setLocalizesFormat: programmatically, the setting will be ignored. The solution is that you have to archive it to an NSData and back using NSArchiver and NSUnarchiver after calling setLocalizesFormat:. This gets the setting to stick, but erases the other formatting information, so you should do this before setting the format the number will use.

Here's a code snippet that I used to get a localized number. I wanted to display the size of a file and make it show up as "1.0 KB (1,024 bytes)", and I wanted the decimal point and thousands separator to show up correctly with different number settings. Here's the code I used:

NSNumberFormatter *nf = [[[NSNumberFormatter alloc] init] autorelease];

[nf setLocalizesFormat:YES];
nf = [NSUnarchiver unarchiveObjectWithData:
[NSArchiver archivedDataWithRootObject:nf]];
[nf setFormat:@"0"];
[nf setHasThousandSeparators:YES];

[sizeField setStringValue:[NSString stringWithFormat:@"%@ (%@ %@)",
[item stringRepresentationOfSize],
[nf stringForObjectValue:[NSNumber numberWithUnsignedLong:[item sizeInBytes]]],
NSLocalizedString(@"bytes",@"")]];

In this code snippet, item is an object that represents a file, and its method sizeInBytes returns an unsigned long of its size, in bytes. Its method stringRepresentationOfSize returns a string containing the nicely formatted size, i.e. "1.0 KB". The relevant line in that method is:

[NSString localizedStringWithFormat:@"%.1f %@",humanReadableSize,unit];

where humanReadableSize is the size, divided by the appropriate number to convert it to KB, MB, etc. and unit is an NSString containing the unit (KB, MB, etc.).

On Sunday, May 5, 2002, at 01:52 PM, Ondra Cada wrote:

On Sunday, May 5, 2002, at 08:23 , Isaac Sherman wrote:

In OS X's System Prefs, the International panel contains the locale settings
for numbers, dates, and time. Does Cocoa have a formatting routine for
numbers and dates that will take into account these various locale settings?

I would like to display numbers, currency, and dates formatted in accordance
with the machine's International settings.

I'd look at NSUserDefaults. It looks like it does exactly what you're
looking for.

Well, yes and no. Via NSUserDefaults one can get all the values and settings; Cocoa though supports many localizable things directly. Just search the documentation: I bet there are some services in NSCalendarDate,
also NSNumberFormatter and NSDateFormatter IIRC use the locale information extensively.

(Caveat: I haven't checked any of them myself though: living in country which was never explicitly supported by NeXTStep nor Mac OS X I've always done all formatting myself, so I can't say how properly it works).
---
Ondra Cada
OCSoftware: email@hidden http://www.ocs.cz
2K Development: email@hidden http://www.2kdevelopment.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
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.
_______________________________________________
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.

References: 
 >Re: Formatting numbers, currency, and dates based on locale (From: Ondra Cada <email@hidden>)

  • Prev by Date: GNUStep-based OS
  • Next by Date: Re: Building OmniFrameworks with PB 2.0
  • Previous by thread: Re: Formatting numbers, currency, and dates based on locale
  • Next by thread: Main event loop in non-main thread, Quartz Compositor
  • Index(es):
    • Date
    • Thread