开发者

Objective C, math functions: round a Float32 with two or three decimals

开发者 https://www.devze.com 2023-02-14 04:58 出处:网络
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.

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

精彩评论

暂无评论...
验证码 换一张
取 消