double testNum =3.7999;
NSNumberFormatter*formatter = [[NSNumberFormatteralloc] init];
typedef CF_ENUM(CFIndex, CFNumberFormatterRoundingMode) {
kCFNumberFormatterRoundCeiling = 0, //四舍五入,直接输出4
kCFNumberFormatterRoundFloor = 1 , //保留小数输出3.8
kCFNumberFormatterRoundDown = 2, //加上了人民币标志,原值输出¥3.8
kCFNumberFormatterRoundUp = 3, //本身数值乘以100后用百分号表示,输出380%
kCFNumberFormatterRoundHalfEven = 4,//输出3.799999999E0
kCFNumberFormatterRoundHalfDown = 5,//原值的中文表示,输出三点七九九九。。。。
kCFNumberFormatterRoundHalfUp = 6//原值中文表示,输出第四
----------------------------------------------------------------------------------
/* typedef NS_ENUM(NSUInteger, NSNumberFormatterStyle) {
NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle,
NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle,
NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle,
NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle,
NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle,
NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle,
NSNumberFormatterOrdinalStyle NS_ENUM_AVAILABLE(10_11, 9_0) = kCFNumberFormatterOrdinalStyle,
NSNumberFormatterCurrencyISOCodeStyle NS_ENUM_AVAILABLE(10_11, 9_0) = kCFNumberFormatterCurrencyISOCodeStyle,
NSNumberFormatterCurrencyPluralStyle NS_ENUM_AVAILABLE(10_11, 9_0) = kCFNumberFormatterCurrencyPluralStyle,
NSNumberFormatterCurrencyAccountingStyle NS_ENUM_AVAILABLE(10_11, 9_0) = kCFNumberFormatterCurrencyAccountingStyle,
};*/
----------------------------------------------------------------
例子:
formatter.numberStyle = kCFNumberFormatterRoundHalfDown;
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_Hans"];//
formatter.locale = locale;
NSString*string = [formatter stringFromNumber:NSNumbernumberWithDouble: testNum]];
NSLog(@"%@",string);};
注意:必须手机系统语言为中文的前提下才可用,如果是非中文环境需要加上中文输出限制.为准确输出,建议使用double类型