简体   繁体   English

四舍五入到最近的10号

[英]Rounding floats to nearest 10th

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

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 %d it will get printed as rounded to the nearest integer: 请注意,如果使用%d执行此操作,它将打印为四舍五入到最接近的整数:

>>> print "%d" % (21.3331,)
21

See the String Formatting Operations docs for more on how the % formatting works. 有关%格式设置的工作原理的更多信息,请参阅字符串格式化操作文档

Here's as simple one. 这是一个简单的问题。

roundf(1.345 * 100) / 100

You simply multiply it by 100 before dividing it by 100 which preserves the value to 2 decimal places. 您只需将它乘以100,然后除以100,将值保留为2位小数。 This should be much more efficient than transforming it to a string and back. 这应该比将其转换为字符串和返回更有效。

You're converting to int . 你正在转换为int That means your result is an int . 这意味着你的结果是一个int int s don't have fractional parts. int s没有小数部分。 Also, "%d" means format as int , so it will implicitly convert to int before printing. 此外, "%d"表示格式为int ,因此它将在打印前隐式转换为int Use "%f" in your format string. 在格式字符串中使用"%f"

def add(op1,op2):
    result = round(op1 + op2, 1)
    print("")
    print ("Output: %0.1f + %0.1f = %0.1f" % (op1, op2, result))

There are two things here: 这里有两件事:

  1. The numbers that are being calculated, do they have a fractional component? 正在计算的数字,它们是否有一个小数部分? Are they integers (whole numbers), or floats (with a decimal point). 它们是整数(整数)还是浮点数(带小数点)。

  2. How do I display them? 我该如何显示它们?

Its important to distinguish between both: 区分两者很重要:

  1. 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. 这是结果的内部类型。

  2. If you do any calculation on two floats (numbers with a decimal point), the result will be a float. 如果对两个浮点数(带小数点的数字)进行任何计算,结果将为浮点数。

  3. If you an operation with an integer and a float, the result will be a float. 如果使用整数和浮点数运算,结果将是浮点数。

The round function changes the precision of the float (how many decimals are significant for you). round函数改变float的精度 (对你来说有多少小数)。 So far we are not talking about how things are displayed or printed . 到目前为止,我们还没有讨论如何显示打印事物。

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. 因此,请确保您在正确的地方做正确的事情。

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

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