Is there any easy way to round a float with one digit number in objective c?

大兔子大兔子 提交于 2019-12-02 16:04:37

问题


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?


回答1:


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




回答2:


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



回答3:


    NSString* strr=[NSString stringWithFormat: @"%.1f", 3.666666];
    NSLog(@"output is:  %@",strr);

output is:3.7

    float fCost = [strr floatValue];



回答4:


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



回答5:


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; 



回答6:


You can use

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

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




回答7:


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


来源:https://stackoverflow.com/questions/16332849/is-there-any-easy-way-to-round-a-float-with-one-digit-number-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!