xcode 如何更改 iOS 的小数位数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7531095/
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
How do I change the number of decimal places iOS?
提问by iOS developer
I have a simple calculator app and I want it to be so that if the answer requires no decimal places, there are none, just whole numbers. If the answer was 2, I don't want it to say 2.000000, it should say 2. If it requires one decimal place, it should show to one decimal place for example 12.8 instead of 12.80. How would I do this? Here is my code. btw, this is from a tutorial at http://www.youtube.com/watch?v=Ihw0cfNOrr4, not my own work.
我有一个简单的计算器应用程序,我希望它是这样的,如果答案不需要小数位,则不需要,只有整数。如果答案是 2,我不想它说 2.000000,它应该说 2。如果它需要一位小数,它应该显示一位小数,例如 12.8 而不是 12.80。我该怎么做?这是我的代码。顺便说一句,这是来自http://www.youtube.com/watch?v=Ihw0cfNOrr4的教程,不是我自己的作品。
viewcontroller.h
视图控制器.h
#import <UIKit/UIKit.h>
interface calcViewController : UIViewController {
float result;
IBOutlet UILabel *calculatorScreen;
int currentOperation;
float currentNumber;
}
-(IBAction)buttonDigitPressed:(id)sender;
-(IBAction)buttonOperationPressed:(id)sender;
-(IBAction)cancelInput;
-(IBAction)cancelOperation;
@end
in the .m
在他们之中
#import "calcViewController.h"
@implementation calcViewController
-(IBAction)buttonDigitPressed:(id)sender {
currentNumber = currentNumber *10 + (float)[sender tag];
calculatorScreen.text = [NSString stringWithFormat:@"%2f", currentNumber];
}
-(IBAction)buttonOperationPressed:(id)sender {
if (currentOperation ==0) result = currentNumber;
else {
switch (currentOperation) {
case 1:
result = result + currentNumber;
break;
case 2:
result = result - currentNumber;
break;
case 3:
result = result * currentNumber;
break;
case 4:
result = result / currentNumber;
break;
case 5:
currentOperation = 0;
break;
}
}
currentNumber = 0;
calculatorScreen.text = [NSString stringWithFormat:@"%2f", result];
if ([sender tag] ==0) result=0;
currentOperation = [sender tag];
}
-(IBAction)cancelInput {
currentNumber =0;
calculatorScreen.text = @"0";
}
-(IBAction)cancelOperation {
currentNumber = 0;
calculatorScreen.text = @"0";
currentOperation = 0;
}
回答by Caleb
One way is to use NSNumberFormatter to format your result instead of NSString's -stringWithFormat:
:
一种方法是使用 NSNumberFormatter 来格式化你的结果而不是 NSString 的-stringWithFormat:
:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:requiredDigits];
[formatter setMinimumFractionDigits:0];
NSString *result = [formatter stringFromNumber:[NSNumber numberWithFloat:currentNumber];
回答by user2824204
The easiest way is just use [[NSNumber numberWithFloat:] stringValue]
最简单的方法就是使用 [[NSNumber numberWithFloat:] stringValue]
Example:
例子:
float someFloatValue;
NSString floatWithoutZeroes = [[NSNumber numberWithFloat:someFloatValue] stringValue];
回答by Yogesh Suthar
If we use %g
in place of %f
will truncate all zeros after decimal point.
如果我们使用%g
in place of%f
将截断小数点后的所有零。
For example
例如
[NSString stringWithFormat:@"%g", 1.201000];
Output
输出
1.201
回答by julia_v
I had the same problem. This is the code snippet that solved it (looks a lot like Caleb's solution, but that one didn't work for me, so I had to add an extra line):
我有同样的问题。这是解决它的代码片段(看起来很像 Caleb 的解决方案,但那个对我不起作用,所以我不得不添加额外的一行):
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = kCFNumberFormatterDecimalStyle;
numberFormatter.maximumFractionDigits = 20;
numberFormatter.minimumFractionDigits = 0;
NSNumber *number = [[NSNumber alloc]init];
NSString *numberString = [numberFormatter stringFromNumber:number];
We can use another numberStyle and override its basic appearance, setting desired properties of an NSNumberFormatter.
我们可以使用另一个 numberStyle 并覆盖它的基本外观,设置 NSNumberFormatter 的所需属性。
回答by Fury
This should work
这应该工作
NSString *result = [@(currentNumber) description];