【问题标题】:Convert string to float without leading and trailing zeros将字符串转换为浮点数,不带前导零和尾随零
【发布时间】:2022-09-29 22:10:31
【问题描述】:

我以字符串格式接收数据:

input1 = \",1\"
input2 = \"1,\"

我需要将它转换为浮点数并在我的测试中验证它。 我期待这种转换:

\",1\" -> 0.1
\"1,\" -> 1.0

不使用正则表达式可以吗?

标签: python


【解决方案1】:

您可以使用replace(), 替换为.,然后转换为float

float(input1.replace(',','.'))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 2023-01-09
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多