Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit a double to two decimal places without trailing zeros

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; 
like image 624
testing Avatar asked Oct 26 '10 17:10

testing


People also ask

How do you get rid of double trailing zeros?

Use a DecimalFormat object with a format string of "0. #".

How do you get rid of trailing zero in decimals?

1. Using Decimal Numbers by Format Cells. First you have to select the cell in which you want to remove trailing zeros after the decimal point, right-click to the Format Cells from the context menu.

How do you double only show two decimal places?

format(“%. 2f”) We also can use String formater %2f to round the double to 2 decimal places. However, we can't configure the rounding mode in String.

How do you keep a float up to 2 decimal places?

format("%. 2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.


1 Answers

Try adding the following line, when configuring your formatter:

    [doubleValueWithMaxTwoDecimalPlaces setMaximumFractionDigits:2]; 
like image 131
Chris Gummer Avatar answered Sep 28 '22 16:09

Chris Gummer