【问题标题】:Limit a double to two decimal places without trailing zeros将双精度数限制为两位小数,不带尾随零
【发布时间】:2010-10-26 17:15:49
【问题描述】:

我读过thisthat。我正是想要这个:

1.4324 => "1.43"
9.4000 => "9.4"
43.000 => "43"

9.4 => “9.40”(错误)
43.000 => “43.00”(错误)

两个问题的答案都指向NSNumberFormatter。所以它应该很容易实现,但对我来说不是。

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 20)];

    NSNumberFormatter *doubleValueWithMaxTwoDecimalPlaces = [[NSNumberFormatter alloc] init];
    [doubleValueWithMaxTwoDecimalPlaces setNumberStyle:NSNumberFormatterDecimalStyle];
    [doubleValueWithMaxTwoDecimalPlaces setPaddingPosition:NSNumberFormatterPadAfterSuffix];
    [doubleValueWithMaxTwoDecimalPlaces setFormatWidth:2];

    NSNumber *myValue = [NSNumber numberWithDouble:0.01234];
    //NSNumber *myValue = [NSNumber numberWithDouble:0.1];

    myLabel.text = [doubleValueWithMaxTwoDecimalPlaces stringFromNumber:myValue];

    [self.view addSubview:myLabel];
    [myLabel release];
    myLabel = nil;
    [doubleValueWithMaxTwoDecimalPlaces release];
    doubleValueWithMaxTwoDecimalPlaces = nil;
}

我也试过了

NSString *resultString = [NSString stringWithFormat: @"%.2lf", [myValue doubleValue]];
NSLog(@"%@", resultString);

那么如何格式化最多两位小数的双精度值?如果最后一个位置包含零,则应省略零。

解决方案:

NSNumberFormatter *doubleValueWithMaxTwoDecimalPlaces = [[NSNumberFormatter alloc] init];
[doubleValueWithMaxTwoDecimalPlaces setNumberStyle:NSNumberFormatterDecimalStyle];
[doubleValueWithMaxTwoDecimalPlaces setMaximumFractionDigits:2];
NSNumber *myValue = [NSNumber numberWithDouble:0.01234];
NSLog(@"%@",[doubleValueWithMaxTwoDecimalPlaces stringFromNumber:myValue]];
[doubleValueWithMaxTwoDecimalPlaces release];
doubleValueWithMaxTwoDecimalPlaces = nil;

【问题讨论】:

  • 您要四舍五入吗?那应该是 1.4363 => "1.43" 还是 "1.44"?
  • 我认为这是有道理的。
  • 别忘了释放 doubleValueWithMaxTwoDecimalPlaces...

标签: iphone objective-c cocoa-touch decimal nsnumberformatter


【解决方案1】:

在配置格式化程序时尝试添加以下行:

    [doubleValueWithMaxTwoDecimalPlaces setMaximumFractionDigits:2];

【讨论】:

    【解决方案2】:
     NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
    [numberFormatter setRoundingMode:NSNumberFormatterRoundDown];
    [numberFormatter setMinimumFractionDigits:2];
    numberFormatter.positiveFormat = @"0.##";
    NSNumber *num = @(total_Value);
    

    【讨论】:

    • 请解释你的答案
    • @SahilMittal 上面代码 NSNumberFormatterRoundDown 返回值不取整,则 positiveFormat 只给出两个小数点值。
    【解决方案3】:

    如何从字符串末尾修剪掉不需要的字符?:

    NSString* CWDoubleToStringWithMax2Decimals(double d) {
        NSString* s = [NSString stringWithFormat:@"%.2f", d];
        NSCharacterSet* cs = [NSCharacterSet characterSetWithCharacterInString:@"0."];
        NSRange r = [s rangeOfCharacterInSet:cs
                                     options:NSBackwardsSearch | NSAnchoredSearch];
        if (r.location != NSNotFound) {
          s = [s substringToIndex:r.location];
        }
        return s;
    }
    

    【讨论】:

    • 即使此解决方案有效,但仍有更好的解决方案(请参阅已接受的答案)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 2012-02-12
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多