0

Yes. You are right. Of Course this is a duplicate question. Before flag my question, please continue reading below.

I want to round a float value, which is

56.6748939 to 56.7
56.45678 to 56.5
56.234589 to 56.2

Actually it can be any number of decimal precisions. But I want to round it to nearest value. (If it is greater than or equal to 5, then round up and if not, then round down).

I can do that with the below code.

float value = 56.68899
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init];
[numberFormatter setMaximumFractionDigits:1];
[numberFormatter setRoundingMode:NSNumberFormatterRoundUp];

NSString *roundedString = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:value]];
NSNumber *roundedNumber = [NSNumber numberFromString:roundedString];
float roundedValue = [roundedNumber floatValue];

Above code looks like a long process. I have several numbers to round off. So this process is hard to convert a float value into NSNumber and to NSString and to NSNumber and to float.

Is there any other easy way to achieve what I asked ?

I still have a doubt in the above code. It says roundUp. So when it comes to roundDown, will it work?

Dinesh Raja
  • 8,501
  • 5
  • 42
  • 81

7 Answers7

6

Can't you simply multiply by 10, round the number, then divide by 10?

Meoiswa
  • 666
  • 4
  • 12
  • for any number, being X the whole, Y the first decimal, and Z the third decimal: X,YZ; multiply by 10 to get XY,Z; then round up or down; if Z > 5 then Y++ and Z=0, if Z<5 then Z=0; afterwards divide by 10 to obtain X,Y – Meoiswa May 02 '13 at 08:12
  • In other words, multiply by ten to carry the decimal over to the whole part, round to have the fractional part become 0 and affect the decimal (now first digit of whole part) then divide by 10 to have the decimal back in place. – Meoiswa May 02 '13 at 08:15
  • 4
    Why don't you just give the code: `roundedValue = roundf(value * 10.0)/10.0;` – Martin R May 02 '13 at 08:19
  • +1 for martin code and also easy answer elaborated in a hard way. – Dinesh Raja May 02 '13 at 08:22
  • 3
    Because I believe when the user has truly understood he can code by himself. – Meoiswa May 02 '13 at 09:02
3

Try

CGFloat float1 = 56.6748939f;
CGFloat float2 = 56.45678f;

NSLog(@"%.1f %.1f",float1,float2);

56.7 56.5

EDIT :

float value = 56.6748939f;
NSString *floatString = [NSString stringWithFormat:@"%.1f",floatValue];
float roundedValue = [floatString floatValue];
Anupdas
  • 10,211
  • 2
  • 35
  • 60
1
    NSString* strr=[NSString stringWithFormat: @"%.1f", 3.666666];
    NSLog(@"output is:  %@",strr);

output is:3.7

    float fCost = [strr floatValue];
Prakash Desai
  • 511
  • 3
  • 14
0

This works for me

NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:1];
[formatter setMinimumFractionDigits:0];

CGFloat firstnumber = 56.6748939;
NSString *result1 = [formatter stringFromNumber:[NSNumber numberWithFloat:firstnumber]];

NSLog(@"RESULT #1: %@",result1);

CGFloat secondnumber = 56.45678;
NSString *result2 = [formatter stringFromNumber:[NSNumber numberWithFloat:secondnumber]];

NSLog(@"RESULT #2: %@",result2);

CGFloat thirdnumber = 56.234589;
NSString *result3 = [formatter stringFromNumber:[NSNumber numberWithFloat:thirdnumber]];

NSLog(@"RESULT #2: %@",result3);
epalleva
  • 113
  • 1
  • 1
  • 5
0

You don't want float, because that only gives you six or seven digits precision. You also don't want CGFloat, because that only gives you six or seven digits precision except on an iPad Air or iPhone 5s. You want to use double.

Rounding to one digit is done very simply:

double x = 56.6748939;
double rounded = round (10 * x) / 10; 
gnasher729
  • 51,477
  • 5
  • 75
  • 98
0

You can use

[dictionaryTemp setObject:[NSString stringWithFormat:@"%.1f",averageRatingOfAllOrders] forKey:@"AvgRating"];

%.1f will give us value 2.1 only one digit after decimal point.

Chandni
  • 692
  • 1
  • 10
  • 25
0

Try this : This will round to any value not limited by powers of 10.

extension Double {
    func roundToNearestValue(value: Double) -> Double {
        let remainder = self % value
        let shouldRoundUp = remainder >= value/2 ? true : false
        let multiple = floor(self / value)
        let returnValue = !shouldRoundUp ? value * multiple : value * multiple + value
        return returnValue
    }
}