我想将'300万','24千','35万'等这些类型的值转换为数字。在 JavaScript(所以我可以在客户端)或 PHP(服务器端)中。有这个功能吗?像 PHP 的 strtotime()
函数?
input output
3 million 3000000
24 thousand 24000
350 thousand 350000
最佳答案
你也想把它们结合起来吗?
function convert(num) {
function mapGroups(str) {
if (/million/.test(str)) {
return str.match(/\d+/)[0] * 1000000;
}
if (/thousand/.test(str)) {
return str.match(/\d+/)[0] * 1000;
}
if (/hundred/.test(str)) {
return str.match(/\d+/)[0] * 100;
}
return +str;
}
return num.match(/(\d+)( ?\w* ?)/g)
.map(mapGroups)
.reduce(function(p, c) { return p + c });
}
console.log(convert("3 million 240 thousand 7 hundred 54")) //3240754
console.log(convert("5 millions 7 hundreds 54")) //5000754
这是非常基础的,但您确实可以掌握。
关于javascript - 将英文数字转换为其数字值(忽略小数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31586924/