How to format double value into string with 2 decimal places after dot and with separators of thousands?

扶醉桌前 提交于 2019-12-12 18:56:47

问题


I have a double value in Objective-C and I need to place its value into label. How should I format it into string with 2 decimal places after dot and with separators of thousands?


回答1:


Use NSNumberFormatter, for example:

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setPositiveFormat:@"#,##0.00" ];

NSString *string = [formatter stringFromNumber:[ NSNumber numberWithDouble:1234567.8901 ] ];
// string is now "1,234,567.89"
[formatter release];


来源:https://stackoverflow.com/questions/7058600/how-to-format-double-value-into-string-with-2-decimal-places-after-dot-and-with

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!