Objective C, math functions: round a Float32 with two or three decimals
I'm developing an iPhone app.
I want to round a Float32 number like this 3.124 to this 3.12. Or a number like 4.2258 to 4.226.
How can I do that?
I want to hold 开发者_开发知识库this value on a Float32 variable or on a float variable.
float x = 4.2258;
x = x * 100;
roundf(x);
x = x / 100;
That's one option. You can also use NSNumberFormatter to get what you want.
Which way do you want to round?
x = 0.01 * floorf(0.5 + 100.0 * x);
精彩评论