在 Python 中删除小数点后的尾随零 [英] Remove trailing zeros after the decimal point in Python

查看:73
本文介绍了在 Python 中删除小数点后的尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Python 2.7.我需要在末尾替换 "0" 字符串.

比如说,a = "2.50":

 a = a.replace('0', '')

我得到 a = 2.5,我对这个结果没问题.

现在 a = "200":

 a = a.replace('0', '')

我得到 a = 2,这个输出是我同意的设计.但我希望输出 a = 200.

其实我看的是

<块引用>

当末尾小数点后的任何值是"0"时替换那个"0"没有值.

以下是示例,我期待结果.

IN: a = "200"输出:a = 200在:a =150"输出:a = 150在:a = 2.50输出:a = 2.5在:a =1500"出:a = 1500在:a =1500.80"输出:a = 1500.8在:a =1000.50"输出:a = 1000.5

不是一个值是字符串.

注意:有时 a = 100LLa = 100.50mt.

解决方案

您可以使用正则表达式来做到这一点:

导入重新rgx = re.compile(r'(?:(\.)|(\.\d*?[1-9]\d*?))0+(?=\b|[^0-9])')b = rgx.sub('\2',a)

其中b是从a中去除小数点后的尾零的结果.

我们可以把它写成一个很好的函数:

导入重新tail_dot_rgx = re.compile(r'(?:(\.)|(\.\d*?[1-9]\d*?))0+(?=\b|[^0-9])')def remove_tail_dot_zeros(a):返回tail_dot_rgx.sub(r'\2',a)

现在我们可以测试一下:

<预><代码>>>>remove_tail_dot_zeros('2.00')'2'>>>remove_tail_dot_zeros('200')'200'>>>remove_tail_dot_zeros('150')'150'>>>remove_tail_dot_zeros('2.59')'2.59'>>>remove_tail_dot_zeros('2.50')'2.5'>>>remove_tail_dot_zeros('2.500')'2.5'>>>remove_tail_dot_zeros('2.000')'2'>>>remove_tail_dot_zeros('2.0001')'2.0001'>>>remove_tail_dot_zeros('1500')'1500'>>>remove_tail_dot_zeros('1500.80')'1500.8'>>>remove_tail_dot_zeros('1000.50')'1000.5'>>>remove_tail_dot_zeros('200.50mt')'200.5 公吨'>>>remove_tail_dot_zeros('200.00mt')'200 吨'

I am using Python 2.7. I need to replace "0" string at the end.

Say, a = "2.50":

 a = a.replace('0', '')

I get a = 2.5, and I am fine with this result.

Now a = "200":

 a = a.replace('0', '')

I get a = 2, and this output is as per design I agreed. But I expect the output a = 200.

Actually what I am looking is

when any value after decimal point at the end is "0" replace that "0" with None value.

Below are the examples and I am expecting results.

IN: a = "200"
Out: a = 200
In: a = "150"
Out: a = 150
In: a = 2.50
Out: a = 2.5
In: a = "1500"
Out: a = 1500
In: a = "1500.80"
Out: a = 1500.8
In: a = "1000.50"
Out: a = 1000.5

Not a value is string.

Note: sometimes a = 100LL or a = 100.50mt.

解决方案

You can do this using a regular expression:

import re

rgx = re.compile(r'(?:(\.)|(\.\d*?[1-9]\d*?))0+(?=\b|[^0-9])')

b = rgx.sub('\2',a)

Where b is the result of removing tailing zeros after the decimal point from a.

We can write this in a nice function:

import re

tail_dot_rgx = re.compile(r'(?:(\.)|(\.\d*?[1-9]\d*?))0+(?=\b|[^0-9])')

def remove_tail_dot_zeros(a):
    return tail_dot_rgx.sub(r'\2',a)

And now we can test this:

>>> remove_tail_dot_zeros('2.00')
'2'
>>> remove_tail_dot_zeros('200')
'200'
>>> remove_tail_dot_zeros('150')
'150'
>>> remove_tail_dot_zeros('2.59')
'2.59'
>>> remove_tail_dot_zeros('2.50')
'2.5'
>>> remove_tail_dot_zeros('2.500')
'2.5'
>>> remove_tail_dot_zeros('2.000')
'2'
>>> remove_tail_dot_zeros('2.0001')
'2.0001'
>>> remove_tail_dot_zeros('1500')
'1500'
>>> remove_tail_dot_zeros('1500.80')
'1500.8'
>>> remove_tail_dot_zeros('1000.50')
'1000.5'
>>> remove_tail_dot_zeros('200.50mt')
'200.5mt'
>>> remove_tail_dot_zeros('200.00mt')
'200mt'

这篇关于在 Python 中删除小数点后的尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆