如何连接四舍五入数字的字符串 python?
How do I concatenate strings of rounded numbers python?
我无法找出在 python 中连接字符串的方式中的错误。
目标是将数字格式化为一个字符串,我可以用一致的长度打印它。
我写了下面的代码:
def numPrint(number,roundplace):
num = round(number, roundplace)
if num > 0:
output = ('+' + str(num))
elif num < 0:
output = (str(num))
else:
output = (' 0.' + '0' * roundplace)
if len(output) < (3 + roundplace):
output2 = (output + '0')
else:
output2 = output
return output2
print(numPrint(0.001, 3))
print(numPrint(0, 3))
print(numPrint(-0.0019, 3))
print(numPrint(-0.01, 3))
print(numPrint(0.1, 3))
我希望它打印:
+0.001
0.000
-0.002
-0.010
+0.100
不过,我得到了
+0.001
0.000
-0.002
-0.010
+0.10
如何将“0”添加到最后一个以使其正常工作?
您可以像这样使用内置格式函数:
print("{:+.4f}".format(0.09))
在此处查看有关格式规范的更多详细信息:
https://docs.python.org/3.4/library/string.html#formatexamples
试试这个:
def num_print(number, roundplace):
sign = '+' if number != 0 else ' '
output = '{num:{sign}.{rp}f}'.format(num=number, sign=sign, rp=roundplace) # python3
# output = f'{number:{sign}.{roundplace}f}' # python 3.6 +
return output
+0.001
0.000
-0.002
-0.010
+0.100
这会起作用:
def numPrint(number,roundplace):
num = round(number, roundplace)
if num > 0:
output = ('+' + str(num))
elif num < 0:
output = (str(num))
else:
output = (' 0.' + '0' * roundplace)
while len(output) < (3 + roundplace): # previously you stopped after adding one trailing 0
output = (output + '0')
return output
print(numPrint(0.001, 3))
print(numPrint(0, 3))
print(numPrint(-0.0019, 3))
print(numPrint(-0.01, 3))
print(numPrint(0.1, 3))
如果只想显示数字,可以使用字符串格式。
def format_number(number, minimum_digits=1, decimal_places=2):
format_string = '{{: {}.{}f}}'.format(minimum_digits, decimal_places)
result = format_string.format(number)
if number:
result = result.replace(' ', '+', 1) # Technically you can use '+' above but your desired output requires zero to have no sign
return result
print(format_number(0.001, minimum_digits=1, decimal_places=3))
print(format_number(0, minimum_digits=1, decimal_places=3))
print(format_number(-0.0019, minimum_digits=1, decimal_places=3))
print(format_number(-0.01, minimum_digits=1, decimal_places=3))
print(format_number(0.1, minimum_digits=1, decimal_places=3))
+0.001
0.000
-0.002
-0.010
+0.100
您只是忘了将 output2
的零相乘:
if len(output) < (3 + roundplace):
output2 = (output + ('0'*(3 + roundplace - len(output))))
else:
output2 = output
或者如果您不介意使用内置函数:
output2 = output.ljust(3 + roundplace, '0')
尝试使用 .format()
字符串方法,它非常有用和方便。
def numPrint(number,roundPlace):
return "{{:+.{}f}}".format(roundPlace).format(number)
对于 Python 3.6+,您可以使用 f-string:
def num_print(number, round_place):
return f"{number:{'+' if number else ' '}.{round_place}f}"
(我故意将您的函数名称及其第二个参数从 numPrint
和 roundPlace
更改为 num_print
和 round_place
与 PEP 8 - Style Guide: Function and Variable Names.)
的一致性
我无法找出在 python 中连接字符串的方式中的错误。
目标是将数字格式化为一个字符串,我可以用一致的长度打印它。
我写了下面的代码:
def numPrint(number,roundplace):
num = round(number, roundplace)
if num > 0:
output = ('+' + str(num))
elif num < 0:
output = (str(num))
else:
output = (' 0.' + '0' * roundplace)
if len(output) < (3 + roundplace):
output2 = (output + '0')
else:
output2 = output
return output2
print(numPrint(0.001, 3))
print(numPrint(0, 3))
print(numPrint(-0.0019, 3))
print(numPrint(-0.01, 3))
print(numPrint(0.1, 3))
我希望它打印:
+0.001
0.000
-0.002
-0.010
+0.100
不过,我得到了
+0.001
0.000
-0.002
-0.010
+0.10
如何将“0”添加到最后一个以使其正常工作?
您可以像这样使用内置格式函数:
print("{:+.4f}".format(0.09))
在此处查看有关格式规范的更多详细信息:
https://docs.python.org/3.4/library/string.html#formatexamples
试试这个:
def num_print(number, roundplace):
sign = '+' if number != 0 else ' '
output = '{num:{sign}.{rp}f}'.format(num=number, sign=sign, rp=roundplace) # python3
# output = f'{number:{sign}.{roundplace}f}' # python 3.6 +
return output
+0.001
0.000
-0.002
-0.010
+0.100
这会起作用:
def numPrint(number,roundplace):
num = round(number, roundplace)
if num > 0:
output = ('+' + str(num))
elif num < 0:
output = (str(num))
else:
output = (' 0.' + '0' * roundplace)
while len(output) < (3 + roundplace): # previously you stopped after adding one trailing 0
output = (output + '0')
return output
print(numPrint(0.001, 3))
print(numPrint(0, 3))
print(numPrint(-0.0019, 3))
print(numPrint(-0.01, 3))
print(numPrint(0.1, 3))
如果只想显示数字,可以使用字符串格式。
def format_number(number, minimum_digits=1, decimal_places=2):
format_string = '{{: {}.{}f}}'.format(minimum_digits, decimal_places)
result = format_string.format(number)
if number:
result = result.replace(' ', '+', 1) # Technically you can use '+' above but your desired output requires zero to have no sign
return result
print(format_number(0.001, minimum_digits=1, decimal_places=3))
print(format_number(0, minimum_digits=1, decimal_places=3))
print(format_number(-0.0019, minimum_digits=1, decimal_places=3))
print(format_number(-0.01, minimum_digits=1, decimal_places=3))
print(format_number(0.1, minimum_digits=1, decimal_places=3))
+0.001
0.000
-0.002
-0.010
+0.100
您只是忘了将 output2
的零相乘:
if len(output) < (3 + roundplace):
output2 = (output + ('0'*(3 + roundplace - len(output))))
else:
output2 = output
或者如果您不介意使用内置函数:
output2 = output.ljust(3 + roundplace, '0')
尝试使用 .format()
字符串方法,它非常有用和方便。
def numPrint(number,roundPlace):
return "{{:+.{}f}}".format(roundPlace).format(number)
对于 Python 3.6+,您可以使用 f-string:
def num_print(number, round_place):
return f"{number:{'+' if number else ' '}.{round_place}f}"
(我故意将您的函数名称及其第二个参数从 numPrint
和 roundPlace
更改为 num_print
和 round_place
与 PEP 8 - Style Guide: Function and Variable Names.)