Reputation: 1000
I want to know if there is a simple function that I can use such this sample. I have a
float value = 1.12345;
I want to round it with calling something like
float value2 = [roundFloat value:value decimal:3];
NSLog(@"value2 = %f", value2);
And I get "1.123"
Is there any Library
or default function for that or I should write a code block for this type of calculations?
thank for your help in advance
Upvotes: 8
Views: 8908
Reputation: 1278
Here is a simple way to do it:
float numberToRound = 1.12345f;
float remainder = numberToRound*1000.0f - (float)((int)(numberToRound*1000.0f));
if (remainder >= 0.5f) {
numberToRound = (float)((int)(numberToRound*1000.0f) + 1)/1000.0f;
}
else {
numberToRound = (float)((int)(numberToRound*1000.0f))/1000.0f;
}
For an arbitrary decimal place, substitute 1000.0f in the above code with
float mult = powf(10.0f, decimal);
Upvotes: 0
Reputation: 19
//Your Number to Round (can be predefined or whatever you need it to be)
float numberToRound = 1.12345;
float min = ([ [[NSString alloc]initWithFormat:@"%.0f",numberToRound] floatValue]);
float max = min + 1;
float maxdif = max - numberToRound;
if (maxdif > .5) {
numberToRound = min;
}else{
numberToRound = max;
}
//numberToRound will now equal it's closest whole number (in this case, it's 1)
Upvotes: 0
Reputation: 323
try
#import <math.h>
float cutFloat( float number, int decimal) {
number = number*( pow(10,decimal) );
number = (int)number;
number = number/( pow(10,decimal) ) ;
return number;
}
Upvotes: -1
Reputation: 310840
Floating point numbers don't have decimal places, they have binary places. Decimal-radix numbers have decimal places. You can't round floating point numbers to specific numbers of decimal places unless you convert to a decimal radix. No routine, method, function etc., that returns a floating point value can possibly carry out this task.
Upvotes: 1
Reputation: 7493
Note that "Round" is not necessarily as simple a topic as you think. For example
DIY Calculator: Rounding Algorithms 101 lists 16 different methods for rounding a number.
Wikipedia:Rounding covers a lot of the same ground
And Cplusplus has source code for a bunch of Rounding Algorithms that are easy translatable to objective-c
How you want to round will depend on the context of what you are doing with for data.
And I should point out that Stack Overflow already has a plethora of other questions about rounding in objective-c
Upvotes: 0
Reputation: 27073
Using NSLog(@"%f", theFloat)
always outputs six decimals, for example:
float theFloat = 1;
NSLog(@"%f",theFloat);
Output:
1.000000
In other words, you will never get 1.123
by using NSLog(@"%f", theFloat)
.
Cut-off after three decimals:
float theFloat = 1.23456;
float newFLoat = (int)(theFloat * 1000.0) / 1000.0;
NSLog(@"%f",newFLoat);
Output:
1.234000
Round to three decimals (using roundf()
/ lroundf()
/ ceil()
/ floor()
):
float theFloat = 1.23456;
float newFLoat = (int)(roundf(theFloat * 1000.0)) / 1000.0;
NSLog(@"%f",newFLoat);
Output:
1.235000
Round to three decimals (dirty way):
float theFloat = 1.23456;
NSString *theString = [NSString stringWithFormat:@"%.3f", theFloat];
float newFloat = [theString floatValue];
NSLog(@"%@",theString);
NSLog(@"%f",newFloat);
Output:
1.235
1.235000
Upvotes: 7
Reputation: 130072
For printing the value use:
NSLog(@"value2 = %.3f", value2);
Rounding to 3 decimal digits before calculations doesn't really make sense because float
is not a precise number. Even if you round it to 1.123
, it will be something like 1.122999999998
.
Rules:
NSDecimalNumber
or fixed point arithmetics.Upvotes: 3