How can we round off decimal numbers to 2 digits in iphone ?
For example ,
5345 --> 53
570 -- > 57
89523 -- > 90
I tried to use nsformatter for this and here is the code i used ,
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumIntegerDigits:2];
[formatter setRoundingMode: NSNumberFormatterRoundUp];
NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithInt:570]];
[formatter release];
NSLog(@"%@",numberstring);
I keep on getting the value as 70 even after changing the rounding mode to different options. Please help me with this
Thanks,
Vinod.
Use this simple code instead of formatter;
myFloat = roundf(100 * myFloat) / 100.0;
i hope it helps.
Or you can use this
float roundedValue = round(2.0f * number) / 2.0f;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:2];
[formatter setRoundingMode: NSNumberFormatterRoundDown];
NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:roundedValue]];
[formatter release];
Since you're looking to round up the decimal digits, you should actually provide a float for this and set the maximum fractional digits to 2. You're probably looking for this,
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:2];
[formatter setRoundingMode: NSNumberFormatterRoundHalfUp];
NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:570.57018]];
[formatter release];
NSLog(@"%@",numberString);
Rounding Number to 2 Decimal Places in C maybe this link helps.you can the convert the float result back to an nsnumber
Deepak's answer is probably exactly what you need, but I'm just providing another option for the sake of options. I'm assuming you're using NSNumber
values, so someNumber
will be of that type in the example:
NSString *numberString = [NSString stringWithFormat:@"%.2f", [someNumber floatValue]];
// this converts the NSNumber to a float, then formats the float in a string;
NSNumber *formattedNumber = [NSNumber numberWithFloat:[numberString floatValue]];
// this extracts the float from the string and puts it back as an NSNumber
It seems there is no direct way to round off a whole number
But you can still do it. You have to do some extra work to convert the whole number to a fractional number and some trick to remove the "0." from the float value.
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[formatter setRoundingMode:NSNumberFormatterRoundHalfUp];
[formatter setMaximumFractionDigits:2];
int n = 570;
float f = [[NSString stringWithFormat:@"0.%d", n] floatValue];
NSNumber *number = [NSNumber numberWithFloat:f];
NSString *str = [formatter stringFromNumber:number];
[formatter setPositivePrefix:@"0."];
number = [formatter numberFromString:str];
[formatter release];