【问题标题】:treating string with numbers in JavaScript [duplicate]在JavaScript中用数字处理字符串[重复]
【发布时间】:2020-10-19 11:02:22
【问题描述】:

我的代码:

test = '1.111,111'
test2 = parseFloat(test)
test3 = test.replace(/,/g, '.')
console.log(test2)//1.111
console.log(parseFloat(test3))//1.111

我需要在“test”变量中以字符串形式返回的整数。

我想要返回:数字格式的 1.111,111。

我该怎么做?

【问题讨论】:

  • 不应该是test3 = test.replace(/,/g, '')吗?你什么时候会看到十进制逗号?
  • @StackSlave 在许多语言环境中。

标签: javascript


【解决方案1】:

我将把它分成几个步骤让你更好地理解:

test = '1.111,111';
test2 = test.split('.').join(''));
test3 = test2.replace(',', '.');
test4 = parseFloat(test3);

你可以在一行中做到这一点:

test = parseFloat(test.split('.').join('').replace(',', '.'));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 2016-11-24
    • 1970-01-01
    • 2015-11-12
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    相关资源
    最近更新 更多