格式化具有最小和最大小数位数的浮点数
format float with minimum and maximum number of decimals
我想格式化一个最少 2 位和最多 8 位小数的浮点数。我这样做:
def format_btc(btc):
s = locale.format("%.8f", btc).rstrip('0')
if s.endswith(','):
s += '00'
return s
有没有办法只用 format() 函数来实现?
编辑:
示例:左边是浮点数,右边是字符串
1 -> 1,00
1.1 -> 1,10 (I have now realised that my code returns 1,1 for this example; that's a bug)
1.12 -> 1,12
1.123 -> 1,123
1.12345678 -> 1,12345678
1.123456789 -> 1,12345678
1,1234567890 -> 1,12345678
没有。我重新检查了 specification language 以确保。可能的原因:
(理论上)如果小数点后8位有意义,那么删0就删除信息。
(实用)添加仅用于浮点数的第三个数字参数的复杂性并不适用于罕见的增益。
我想格式化一个最少 2 位和最多 8 位小数的浮点数。我这样做:
def format_btc(btc):
s = locale.format("%.8f", btc).rstrip('0')
if s.endswith(','):
s += '00'
return s
有没有办法只用 format() 函数来实现?
编辑:
示例:左边是浮点数,右边是字符串
1 -> 1,00
1.1 -> 1,10 (I have now realised that my code returns 1,1 for this example; that's a bug)
1.12 -> 1,12
1.123 -> 1,123
1.12345678 -> 1,12345678
1.123456789 -> 1,12345678
1,1234567890 -> 1,12345678
没有。我重新检查了 specification language 以确保。可能的原因:
(理论上)如果小数点后8位有意义,那么删0就删除信息。
(实用)添加仅用于浮点数的第三个数字参数的复杂性并不适用于罕见的增益。