将一个double限制为两个小数位,但不包含尾随零 [英] Limit a double to two decimal places without trailing zeros

查看:165
本文介绍了将一个double限制为两个小数位,但不包含尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了。我想要这样:


1.4324 =>1.43

9.4000 =>9.4

43.000 =>43



9.4 =>9.40(错误)

43.000 =>43.00 / p>

在这两个问题中,答案都指向 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;
pre>

解决方案

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

  [doubleValueWithMaxTwoDecimalPlaces setMaximumFractionDigits:2]; 


I read this and that. I want this exactly:

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

9.4 => "9.40" (wrong)
43.000 => "43.00" (wrong)

In both questions the answers points to NSNumberFormatter. So it should be easy to achieve, but not for me.

- (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;
}

I also tried it with

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

So how can I format double values with maximum two decimal places? If the last position contains a zero the zero should be left out.

Solution:

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;

解决方案

Try adding the following line, when configuring your formatter:

    [doubleValueWithMaxTwoDecimalPlaces setMaximumFractionDigits:2];

这篇关于将一个double限制为两个小数位,但不包含尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆