Yes. You're right. Of course, this is a recurring question. Before asking your question, please continue reading below.
I want to round the float value, which
56.6748939 to 56.7
56.45678 to 56.5
56.234589 to 56.2
In fact, it can be any number of decimal prefixes. But I want to round it to the nearest value. (If it is greater than or equal to 5, then round, and if not, then round down).
I can do this with the code below.
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];
The above code looks like a lengthy process. I have a few rooms left. Thus, this process is difficult to convert the float value to NSNumber and NSString and NSNumber and float.
Is there any other easy way to achieve what I asked?
. roundUp. , roundDown, ?