如何从小数中删除所有前导零
How to remove all leading zeros from decimals
我想从小数中删除所有前导零。
例如 0,0000000029 应该改为 29 或者 0,000000710 应该改为 710。
在 python 中实现此目标的最简单方法是什么?
抱歉,如果这是一个明显的问题,但我还没有找到任何具体的答案,而且我是一个绝对的初学者。
编辑:我想将其用于我正在编写的 Binance 加密货币交易机器人。每种货币都有精度,因此始终给出最大浮点长度。例如,当一种货币的价格为 0,00000027 时,我希望将其返回为 27。
这里有一个使用字符串操作的简单方法:
# Your number, defined to 10 decimal places
x = 0.0000001230
# Convert to string using f-strings with 10 decimal precision
out = f'{x:.10f}'
# Split at the decimal and take the second element from that list
out = out.split('.')[1]
# Strip the zeros from the left side of your split decimal
out = out.lstrip('0')
out
>>> '1230'
单线:
out = f'{x:.10f}'.split('.')[1].lstrip('0')
如果你希望最后的结果是整数或浮点数,只需在使用int(out)
或float(out)
后进行转换即可。
编辑:如果你想改变精度(必须固定精度以解决尾随零),你只需要改变出现在 f 字符串中的整数:out = f'{x:.<precision>f}'
.
您可以使用归一化方法去除额外的精度。
>>> print decimal.Decimal('5.500')
5.500
>>> print decimal.Decimal('5.500').normalize()
5.5
为避免去除小数点左侧的零,您可以这样做:
def normalize_fraction(d):
normalized = d.normalize()
sign, digits, exponent = normalized.as_tuple()
if exponent > 0:
return decimal.Decimal((sign, digits + (0,) * exponent, 0))
else:
return normalized
或者更简洁,使用 user7116 建议的量化:
def normalize_fraction(d):
normalized = d.normalize()
sign, digit, exponent = normalized.as_tuple()
return normalized if exponent <= 0 else normalized.quantize(1)
您也可以使用 to_integral(),如此处所示,但我认为以这种方式使用 as_tuple 更易于自我记录。
我在几个案例中测试了这两个;如果您发现有问题,请发表评论。
>>> normalize_fraction(decimal.Decimal('55.5'))
Decimal('55.5')
>>> normalize_fraction(decimal.Decimal('55.500'))
Decimal('55.5')
>>> normalize_fraction(decimal.Decimal('55500'))
Decimal('55500')
>>> normalize_fraction(decimal.Decimal('555E2'))
Decimal('55500')
我想从小数中删除所有前导零。
例如 0,0000000029 应该改为 29 或者 0,000000710 应该改为 710。
在 python 中实现此目标的最简单方法是什么?
抱歉,如果这是一个明显的问题,但我还没有找到任何具体的答案,而且我是一个绝对的初学者。
编辑:我想将其用于我正在编写的 Binance 加密货币交易机器人。每种货币都有精度,因此始终给出最大浮点长度。例如,当一种货币的价格为 0,00000027 时,我希望将其返回为 27。
这里有一个使用字符串操作的简单方法:
# Your number, defined to 10 decimal places
x = 0.0000001230
# Convert to string using f-strings with 10 decimal precision
out = f'{x:.10f}'
# Split at the decimal and take the second element from that list
out = out.split('.')[1]
# Strip the zeros from the left side of your split decimal
out = out.lstrip('0')
out
>>> '1230'
单线:
out = f'{x:.10f}'.split('.')[1].lstrip('0')
如果你希望最后的结果是整数或浮点数,只需在使用int(out)
或float(out)
后进行转换即可。
编辑:如果你想改变精度(必须固定精度以解决尾随零),你只需要改变出现在 f 字符串中的整数:out = f'{x:.<precision>f}'
.
您可以使用归一化方法去除额外的精度。
>>> print decimal.Decimal('5.500')
5.500
>>> print decimal.Decimal('5.500').normalize()
5.5
为避免去除小数点左侧的零,您可以这样做:
def normalize_fraction(d):
normalized = d.normalize()
sign, digits, exponent = normalized.as_tuple()
if exponent > 0:
return decimal.Decimal((sign, digits + (0,) * exponent, 0))
else:
return normalized
或者更简洁,使用 user7116 建议的量化:
def normalize_fraction(d):
normalized = d.normalize()
sign, digit, exponent = normalized.as_tuple()
return normalized if exponent <= 0 else normalized.quantize(1)
您也可以使用 to_integral(),如此处所示,但我认为以这种方式使用 as_tuple 更易于自我记录。
我在几个案例中测试了这两个;如果您发现有问题,请发表评论。
>>> normalize_fraction(decimal.Decimal('55.5'))
Decimal('55.5')
>>> normalize_fraction(decimal.Decimal('55.500'))
Decimal('55.5')
>>> normalize_fraction(decimal.Decimal('55500'))
Decimal('55500')
>>> normalize_fraction(decimal.Decimal('555E2'))
Decimal('55500')