DataFormatter

You use formatters to interpret and create strings that represent other data types, and to validate the text in
text fields and other cells. Formatters are instances of subclasses of the abstract class, NSFormatter. The
Foundation framework provides two concrete subclasses of NSFormatter: NSNumberFormatter and
NSDateFormatter. (Core Foundation provides two equivalent opaque types: CFNumberFormatter and
CFDateFormatter. These are similar but are not toll-free bridged.) You can create a subclass of NSFormatter

for custom formatting.



1--Number Formatters

NSNumberFormatter provides two convenient methods—stringFromNumber: and
numberFromString:—that you an use to create a string representation of a number and to create a number
object from a string respectively. To create a localized string representation of a number without creating a
formatter object, you can use the class method localizedStringFromNumber:numberStyle:.


Use Formatter Styles to Present Numbers With the User’s Preferences

NSNumberFormatter makes it easy for you to format different sorts of number using the settings a user
configured in the International preferences panel in System Preferences. The NSNumberFormatter style
constants—NSNumberFormatterDecimalStyle, NSNumberFormatterCurrencyStyle,
NSNumberFormatterPercentStyle, NSNumberFormatterScientificStyle, or
NSNumberFormatterSpellOutStyle


Listing 1 Formatting a number using a formatter style
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber
numberWithFloat:122344.4563]];

NSLog(@"formattedNumberString: %@", formattedNumberString);
// Output for locale en_US: "formattedNumberString: formattedNumberString:
122,344.453"


Use Format Strings to Specify Custom Formats


Note with the Unicode format string format, you should enclose literal text in the format string between
apostrophes ('').
You specify the format string of a number formatter using setPositiveFormat: and setNegativeFormat:.
Listing 2 (page 6) illustrates how you can format a date using formatter styles.


Listing 2 Formatting a number using a format string
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setPositiveFormat:@"###0.##"];

NSString *formattedNumberString = [numberFormatter stringFromNumber:[NSNumber
numberWithFloat:122344.4563]];
NSLog(@"formattedNumberString: %@", formattedNumberString);
// Output for locale en_US: "formattedNumberString: formattedNumberString:
122,344.45"


G.1 Number Patterns

The NumberElements resource affects how these patterns are interpreted in a localized context. Here are some examples, based on the French locale. The "." shows where the decimal point should go. The "," shows where the thousands separator should go. A "0" indicates zero-padding: if the number is too short, a zero (in the locale's numeric set) will go there. A "#" indicates no padding: if the number is too short, nothing goes there. A "¤" shows where the currency sign will go. The following illustrates the effects of different patterns for the French locale, with the number "1234.567". Notice how the pattern characters ',' and '.' are replaced by the characters appropriate for the locale.

PatternCurrencyText
#,##0.##n/a1 234,57
#,##0.###n/a1 234,567
###0.#####n/a1234,567
###0.0000#n/a1234,5670
00000.0000n/a01234,5670
# ##0.00 ¤EUR1 234,57 €
JPY1 235 ¥

The number of # placeholder characters before the decimal do not matter, since no limit is placed on the maximum number of digits. There should, however, be at least one zero someplace in the pattern. In currency formats, the number of digits after the decimal also do not matter, since the information in the supplemental data (see Appendix C: Supplemental Data) is used to override the number of decimal places — and the rounding — according to the currency that is being formatted. That can be seen in the above chart, with the difference between Yen and Euro formatting.

When parsing using a pattern, a lenient parse should be used; see Lenient Parsing.


Percentages
If you use a format string with a “%” character to format percentages, the results may be confusing. Consider
the following example:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setPositiveFormat:@"0.00%;0.00%;-0.00%"];
NSNumber *four = [NSNumber numberWithFloat:4.0];
NSLog(@"%@", [numberFormatter stringFromNumber:four]);
// output: "400.00%"


If you want to represent a number as a percentage, you should use the NSNumberFormatterPercentStyle
style—this also ensures that percentages are formatted appropriately for the locale:


NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
NSNumber *four = [NSNumber numberWithFloat:4.0];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[numberFormatter setLocale:usLocale];
NSLog(@"en_US: %@", [numberFormatter stringFromNumber:four]);
// output: "en_US: 400%"
NSLocale *faLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"fa_IR"];
[numberFormatter setLocale:faLocale];
NSLog(@"fa_IR: %@", [numberFormatter stringFromNumber:four]);
// output: "fa_IR:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值