欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

字符串格式化/数值小数点

程序员文章站 2024-03-26 12:19:41
...

通过NSNumberFormatter,设置NSNumber输出的格式

NSNumberFormatter *formatter = [[NSNumberFormatteralloc]init];

formatter.numberStyle =NSNumberFormatterDecimalStyle;

NSString *newAmount = [formatterstringFromNumber:[NSNumbernumberWithint:123456789]];

(注意传入参数的数据长度,还可用double类型等)

NSNumberFormatter类有个属性numberStyle,是一个枚举型,设置不同的值可以输出不同的数字格式。该枚举包括:

enum{

    NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle,

    NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle,

    NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle,

    NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle,

    NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle,

    NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle

};

各个枚举对应输出数字格式的效果如下:

123456789

123,456,789

¥123,456,789.00

-539,222,988%

1.23456789E8

一亿二千三百四十五万六千七百八十九