objective-c 在目标 C 中格式化浮点数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2054218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Formatting floats in Objective C
提问by Adam Tootle
I need to format a float (catchy title, he?) to 2 decimal places, but only if those decimal places have values that aren't zero. Example:
我需要将浮点数(吸引人的标题,他?)格式化为 2 个小数位,但前提是这些小数位的值不为零。例子:
I have a NSTextField named 'answer', after I do some math with a couple of floats, I want to assign my 'answerFloat' variable to the 'answer' NSTextField. So far I've got:
我有一个名为“answer”的 NSTextField,在我用几个浮点数做了一些数学运算之后,我想将我的“answerFloat”变量分配给“answer”NSTextField。到目前为止,我有:
[answer setStringValue:[NSString stringWithFormat:@"%.2f", answerFloat]];
But that sets something like 45 to 45.00. I want whole numbers to be displayed without the zeroes, and any decimal numbers to be displayed with their respective decimal values.
但这会将 45 设置为 45.00。我希望显示不带零的整数,以及显示任何十进制数及其各自的十进制值。
Do I need to run some kind of check before giving it to stringWithFormat? Or does NSString offer a way to handle this?
在将其提供给 stringWithFormat 之前,我是否需要进行某种检查?还是 NSString 提供了一种处理方法?
回答by Mark Bessey
Have you tried the %gformat specifier?
您是否尝试过%g格式说明符?
NSLog([NSString stringWithFormat:@"%g, %g", 45.0, 45.5]);
2010-01-12 19:54:38.651 foo[89884:10b] 45, 45.5
2010-01-12 19:54:38.651 foo[89884:10b] 45, 45.5
回答by Mark Suman
The problem with %g is that it doesn't have a way to specify the rounding increment (at least, not that I can find).
%g 的问题在于它没有办法指定舍入增量(至少,我找不到)。
You can use NSNumberFormatter like this to achieve your result with a number that has an undefined number of decimal places.
您可以像这样使用 NSNumberFormatter 来获得具有未定义小数位数的数字的结果。
double none = 5;
double one = 5.1;
double two = 5.01;
double lots = 5.918286558251858392107584219;
NSNumber *numberNone = [NSNumber numberWithDouble:none];
NSNumber *numberOne = [NSNumber numberWithDouble:one];
NSNumber *numberTwo = [NSNumber numberWithDouble:two];
NSNumber *numberLots = [NSNumber numberWithDouble:lots];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.roundingIncrement = [NSNumber numberWithDouble:0.01];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSLog(@"%@",[formatter stringFromNumber:numberNone]);
NSLog(@"%@",[formatter stringFromNumber:numberOne]);
NSLog(@"%@",[formatter stringFromNumber:numberTwo]);
NSLog(@"%@",[formatter stringFromNumber:numberLots]);
Output:
输出:
2012-02-15 16:21:17.469 AwakeFromNib[53043:f803] 5
2012-02-15 16:21:17.470 AwakeFromNib[53043:f803] 5.1
2012-02-15 16:21:17.470 AwakeFromNib[53043:f803] 5.01
2012-02-15 16:21:17.471 AwakeFromNib[53043:f803] 5.92
回答by NSResponder
Look up NSNumberFormatter.
查找 NSNumberFormatter。
回答by mrkj
For the greatest flexibility, you could consider implementing a custom subclass of NSFormatter. There might be other salient tips in the Data Formatting Programming Guide for Cocoa.
为了获得最大的灵活性,您可以考虑实现NSFormatter的自定义子类。Cocoa的数据格式编程指南中可能还有其他重要提示。

