简体   繁体   English

如何将数字舍入到最多 3 位小数?

[英]How to round number to 3 decimals max?

I am trying to round up 5.9999998 to 5.999.我正在尝试将 5.9999998 向上舍入到 5.999。

But I have a problem, If I do round(number) it'll round it up to 6. How can I round a number like this to max 3 decimals?但是我有一个问题,如果我四舍五入(数字),它会将它四舍五入到 6。我怎样才能将这样的数字四舍五入到最多 3 位小数?

You can use this:你可以使用这个:

number = 5.999999998
new_number = int(number * 1e3) / 1e3

Here is a short code.这是一个简短的代码。


x = 4/3

# round up to 3 decimal places
x = round(x, 3)

print(x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM