Python 四舍五入浮动到最接近的第 10 位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19431674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rounding floats to nearest 10th
提问by user2839430
I am trying to round an equation of two float numbers but it outputs without the decimals, it just rounds it to the nearest number for example 21.3 to 21. When I put ", 2" which should set it to round to the nearest 10th.
我试图四舍五入一个包含两个浮点数的方程,但它输出没有小数,它只是将其四舍五入到最接近的数字,例如 21.3 到 21。当我输入“,2”时,应该将其设置为最接近的 10 位。
New code:
新代码:
def add(op1,op2):
result = int(round(op1 + op2, -1))
print("")
print ("Output: %d + %d = %d" % (op1, op2, result))
Output:
输出:
Output: 10 + 10 = 20
NEW
新的
Output:
输出:
Output: 10.3 + 10.9 = 21.0
Code:
代码:
def add(op1,op2):
result = int(round(op1 + op2, 1))
print("")
print ("Output: %0.1f + %0.1f = %0.1f" % (op1, op2, result))
采纳答案by Claudiu
Here's how to round a number to the nearest tenth:
以下是将数字四舍五入到最接近的十分之一的方法:
>>> round(21.3331, 1)
21.3
Here's how to print out a floating point number with one decimal point:
以下是打印带一位小数点的浮点数的方法:
>>> print "%.1f" % (21.3331,)
21.3
Note that if you do it using %dit will get printed as rounded to the nearest integer:
请注意,如果您使用%d它,它将被打印为四舍五入到最接近的整数:
>>> print "%d" % (21.3331,)
21
See the String Formatting Operations docsfor more on how the %formatting works.
回答by jpkotta
You're converting to int. That means your result is an int. ints don't have fractional parts. Also, "%d"means format as int, so it will implicitly convert to intbefore printing. Use "%f"in your format string.
您正在转换为int. 这意味着您的结果是int. ints 没有小数部分。此外,"%d"表示格式为int,因此它将int在打印前隐式转换为。使用"%f"您的格式字符串。
def add(op1,op2):
result = round(op1 + op2, 1)
print("")
print ("Output: %0.1f + %0.1f = %0.1f" % (op1, op2, result))
回答by Burhan Khalid
There are two things here:
这里有两件事:
The numbers that are being calculated, do they have a fractional component? Are they integers (whole numbers), or floats (with a decimal point).
How do I display them?
正在计算的数字是否有小数部分?它们是整数(整数)还是浮点数(带小数点)。
我如何显示它们?
Its important to distinguish between both:
区分两者很重要:
Any operation on two integers will result in an integer; a number that does not have a decimal component. This is the internal type of the result.
If you do any calculation on two floats (numbers with a decimal point), the result will be a float.
If you an operation with an integer and a float, the result will be a float.
对两个整数的任何运算都会产生一个整数;一个没有小数部分的数字。这是结果的内部类型。
如果您对两个浮点数(带小数点的数字)进行任何计算,结果将是一个浮点数。
如果您使用整数和浮点数进行运算,结果将是浮点数。
The roundfunction changes the precisionof the float (how many decimals are significant for you). So far we are not talking about how things are displayedor printed.
该round函数会更改浮点数的精度(对您来说重要的小数位数)。到目前为止,我们还没有讨论如何显示或打印事物。
To print it with a decimal component, you need to format how its displayed, not change how its stored and used, this doesn't make a difference what the type of the number is. Here you can see I am printing an integer with a decimal component, even though it doesn't have one:
要使用十进制组件打印它,您需要格式化它的显示方式,而不是更改它的存储和使用方式,这对数字的类型没有影响。在这里你可以看到我正在打印一个带有小数部分的整数,即使它没有:
>>> i = 10 + 10
>>> i
20
>>> print("{0:.2f}".format(i))
20.00
And if you have a number with a decimal component; then depending on how you display it, various things will happen:
如果您有一个带有小数部分的数字;然后根据您的显示方式,会发生各种情况:
>>> i = 1.24556434532
>>> print("{0:.2f}".format(i))
1.25
>>> print("{0:.1f}".format(i))
1.2
>>> print("{0:.3f}".format(i))
1.246
So make sure you are doing the right thing at the right place.
因此,请确保您在正确的地方做正确的事情。
回答by Brennan
Here's as simple one.
这是一个简单的。
roundf(1.345 * 100) / 100
roundf(1.345 * 100) / 100
You simply multiply it by 100 before dividing it by 100 which preserves the value to 2 decimal places. This should be much more efficient than transforming it to a string and back.
您只需将其乘以 100,然后再除以 100,即可将值保留为小数点后两位。这应该比将其转换为字符串并返回更有效。

