Python:如何将浮点值舍入为1.20而不是1.2

时间:2016-10-02 23:13:26

标签: python-3.x rounding

我发现如果我对某些浮点值使用round函数,可以舍入到两位小数,但有时,例如,$w = "World!"; $h = "Hello ".$w; echo $h; /* * This will result printing out "Hello World"; * The reason for this is because $w is a "Container", * that means the variable is keeping some information in it, basicly: it exists. * Now you can also use this in functions: */ function some_function($full_string){ return $full_string; } echo some_function($h." What a good day!"); Which will result in "Hello World! What a good day!" // I hope this was what you was looking for! 它将返回round(myvalu, 2)而不是1.2但实际上我希望每个返回的值都舍入到两位小数,即1.20而不是1.20。我该怎么办?

1 个答案:

答案 0 :(得分:1)

这是显示数字时你要做的事情。

尝试

'%.2f' % num