Objective-C中的Round双打 [英] Round doubles in Objective-C

查看:102
本文介绍了Objective-C中的Round双打的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的格式为 34.123456789 。如何将其更改为34.123?

I have double number in a format like 34.123456789. How can I change it to 34.123?

我只想要小数点后的3位数。

I just want 3 digits after the decimal point.

推荐答案

您可以使用 [NSString stringWithFormat:@%。3f,d] 将其打印到3位小数。

You can print it to 3 decimal places with [NSString stringWithFormat:@"%.3f",d].

您可以使用 round(d * 1000)/ 1000 大致舍入它,但当然这不能保证是准确的,因为1000不是2的力量。

You can approximately round it with round(d*1000)/1000, but of course this isn't guaranteed to be exact since 1000 isn't a power of 2.

这篇关于Objective-C中的Round双打的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆