Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在最后两点设置小数点_Python - Fatal编程技术网

Python 在最后两点设置小数点

Python 在最后两点设置小数点,python,Python,我不知道如何将小数点设置为最后两个数字 我尝试了这个'{0:.2f}'。格式(a),但这样的'117085.00' 这就是我所拥有的 117085 55688 我想要 1170.85 556.88 所以我需要最后两个数字的一个点。 我不想要新的数字,我只需要设定点 有人能帮我解决这个问题吗我真的是新的您接收数字的方式,它们是您试图打印的数值的100倍。要按您想要的方式对其进行格式化,请在格式化之前将其除以100 In [33]: x = 117085

我不知道如何将小数点设置为最后两个数字

我尝试了这个
'{0:.2f}'。格式(a)
,但这样的
'117085.00'

这就是我所拥有的

117085
55688
我想要

1170.85
 556.88
所以我需要最后两个数字的一个点。 我不想要新的数字,我只需要设定点


有人能帮我解决这个问题吗我真的是新的

您接收数字的方式,它们是您试图打印的数值的100倍。要按您想要的方式对其进行格式化,请在格式化之前将其除以100

In [33]: x = 117085                                                                                                                                                                                                                                                                                                     

In [34]: x/100                                                                                                                                                                                                                                                                                                          
Out[34]: 1170.85
此外,您提供的示例似乎是右对齐的,这意味着右侧的所有线都对齐。如果您想实现这一点,可以使用以下方法:

a=117085
b=55688
打印({0:>7.2f})。格式(a/100))
打印({0:>7.2f})。格式(b/100))
输出:

1170.85
 556.88
编辑:
rjust(7)
转换为格式字符串
>7

让我们分解一下上面使用的格式字符串

{0:>7.2f} # The whole string
{       } # Brackets to denote a processed value
 0        # Take the first argument passed through the `format()` function
  :       # A delimiter to separate the identifier (in this case, 0) from the format notation
   >7     # Right justify this element, with a width of 7
     .2f  # Format the input as a float, with 2 digits to the right of the decimal point
Python显然隐含了一些假设,因此这里有一个较短的替代方案:

{:7.2f} # The whole string
{     } # Brackets to denote a processed value
 :      # A delimiter to separate the identifier (in this case, assumed 0) from the format notation
  7     # justify this element (Right justification by default), with a width of 7
   .2f  # Format the input as a float, with 2 digits to the right of the decimal point

这回答了你的问题吗@约翰:那太糟糕了。
0.01
已经不准确,然后在计算中使用该不准确的数字。最好做
/100
x=117085
x/100
@JohanC例如,
35*0.01
0.35000000003
35/100
0.35
@JohanC不确定舍入,我所记得的是标准保证除法结果是最接近的可表示数。但当你用一个已经不准确的值进行乘法时,你不会得到这个结果。而且
*0.01
的方式一直都是不好的:格式字符串并不总是最清晰易读的东西,但你可以这样做。