带余数的小数点
Decimal point with remainder
我的计算器需要帮助 app.I我使用了两个浮点变量,所以当我做一个练习账户时,例如 6/2 结果是 3.000000(带小数点)而不是 3,因为它是一个浮动变量。但是,如果我做2/6结果是0.33333,如何解决只有有余数才显示小数点的问题。
代码如下:
//Add the number to UILabel
-(IBAction)numberPressed:(UIButton *)sender {
NSString *number = sender.currentTitle;
if (self.typingNumber) {
self.firstLabel.text = [self.firstLabel.text stringByAppendingString:number];
}
else {
self.firstLabel.text = number;
self.typingNumber = YES;
}
}
//Equal method
-(IBAction)equalPress:(id)sender {
self.typingNumber = NO;
self.secondNum = [self.firstLabel.text floatValue];
result = 0;
if ([self.operation isEqualToString:@"+"]) {
result = self.firstNum + self.secondNum;
}
if ([self.operation isEqualToString:@"-"]) {
result = self.firstNum - self.secondNum;
}
if ([self.operation isEqualToString:@"X"]) {
result = self.firstNum * self.secondNum;
}
if ([self.operation isEqualToString:@"/"]) {
result = self.firstNum / self.secondNum;
}
self.firstLabel.text = [NSString stringWithFormat:@"%f" ,result];
}
您可以尝试使用%g
格式规范:
The double argument shall be converted in the style f or e (or in the style F or E in the case of a G conversion specifier), with the precision specifying the number of significant digits. If an explicit precision is zero, it shall be taken as 1. The style used depends on the value converted; style e (or E ) shall be used only if the exponent resulting from such a conversion is less than -4 or greater than or equal to the precision. Trailing zeros shall be removed from the fractional portion of the result; a radix character shall appear only if it is followed by a digit or a '#' flag is present.
来自 Apple 使用的 IEEE printf specification,添加了 强调。
例如:
float a = 6.0 / 2.0;
float b = 2.0 / 6.0;
NSLog(@"%f, %g | %f, %g", a, a, b, b);
产生:
3.000000, 3 | 0.333333, 0.333333
要获得更多控制,请使用 NSNumberFormatter
class,特别是查看 alwaysShowsDecimalSeparator
.
等属性
例如:
NSNumberFormatter *formatter = [NSNumberFormatter new];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
formatter.alwaysShowsDecimalSeparator = NO;
NSLog(@"%@ | %@", [formatter stringFromNumber:@(a)], [formatter stringFromNumber:@(b)]);
产生:
3 | 0.333
(可以使用其他属性控制小数位数等)
我的计算器需要帮助 app.I我使用了两个浮点变量,所以当我做一个练习账户时,例如 6/2 结果是 3.000000(带小数点)而不是 3,因为它是一个浮动变量。但是,如果我做2/6结果是0.33333,如何解决只有有余数才显示小数点的问题。
代码如下:
//Add the number to UILabel
-(IBAction)numberPressed:(UIButton *)sender {
NSString *number = sender.currentTitle;
if (self.typingNumber) {
self.firstLabel.text = [self.firstLabel.text stringByAppendingString:number];
}
else {
self.firstLabel.text = number;
self.typingNumber = YES;
}
}
//Equal method
-(IBAction)equalPress:(id)sender {
self.typingNumber = NO;
self.secondNum = [self.firstLabel.text floatValue];
result = 0;
if ([self.operation isEqualToString:@"+"]) {
result = self.firstNum + self.secondNum;
}
if ([self.operation isEqualToString:@"-"]) {
result = self.firstNum - self.secondNum;
}
if ([self.operation isEqualToString:@"X"]) {
result = self.firstNum * self.secondNum;
}
if ([self.operation isEqualToString:@"/"]) {
result = self.firstNum / self.secondNum;
}
self.firstLabel.text = [NSString stringWithFormat:@"%f" ,result];
}
您可以尝试使用%g
格式规范:
The double argument shall be converted in the style f or e (or in the style F or E in the case of a G conversion specifier), with the precision specifying the number of significant digits. If an explicit precision is zero, it shall be taken as 1. The style used depends on the value converted; style e (or E ) shall be used only if the exponent resulting from such a conversion is less than -4 or greater than or equal to the precision. Trailing zeros shall be removed from the fractional portion of the result; a radix character shall appear only if it is followed by a digit or a '#' flag is present.
来自 Apple 使用的 IEEE printf specification,添加了 强调。
例如:
float a = 6.0 / 2.0;
float b = 2.0 / 6.0;
NSLog(@"%f, %g | %f, %g", a, a, b, b);
产生:
3.000000, 3 | 0.333333, 0.333333
要获得更多控制,请使用 NSNumberFormatter
class,特别是查看 alwaysShowsDecimalSeparator
.
例如:
NSNumberFormatter *formatter = [NSNumberFormatter new];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
formatter.alwaysShowsDecimalSeparator = NO;
NSLog(@"%@ | %@", [formatter stringFromNumber:@(a)], [formatter stringFromNumber:@(b)]);
产生:
3 | 0.333
(可以使用其他属性控制小数位数等)