Iphone 将双精度限制为小数点后两位,不带尾随零

Iphone 将双精度限制为小数点后两位,不带尾随零,iphone,objective-c,cocoa-touch,decimal,nsnumberformatter,Iphone,Objective C,Cocoa Touch,Decimal,Nsnumberformatter,我阅读和阅读。我想要的正是: 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:CGRectMak

我阅读和阅读。我想要的正是:

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;

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

    [doubleValueWithMaxTwoDecimalPlaces setMaximumFractionDigits:2];

从字符串末尾修剪不需要的字符怎么样

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

你要四舍五入吗?这应该是1.4363=>“1.43”或“1.44”?我认为这是有意义的。毕竟,不要忘记使用MaxTwoDecimalPlace发布DoubleValues…即使这个解决方案有效,也有一个更好的解决方案(见公认的答案)pl解释您的问题answer@SahilMittal以上代码NSNumberFormatterRoundDown返回值不带舍入,那么正数形式只给出两个分数点的值。
 NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setRoundingMode:NSNumberFormatterRoundDown];
[numberFormatter setMinimumFractionDigits:2];
numberFormatter.positiveFormat = @"0.##";
NSNumber *num = @(total_Value);