我有这种格式的输入号码:
1.123.355,50
和JavaScript不知道那种格式,所以我需要将其转移到:
1123355.50
有什么想法吗?
答案 0 :(得分:2)
var numberString = '1.123.355,50';
numberString = numberString
.replace(/\./g, '') // replace all separators
.replace(/,/, '.'); // replace comma with dot
var parsed = parseFloat(numberString);
答案 1 :(得分:1)
来自php.js
:
function number_format(number, decimals, dec_point, thousands_sep) {
// example 1: number_format(1234.56);
// returns 1: '1,235'
// example 2: number_format(1234.56, 2, ',', ' ');
// returns 2: '1 234,56'
// example 3: number_format(1234.5678, 2, '.', '');
// returns 3: '1234.57'
// example 4: number_format(67, 2, ',', '.');
// returns 4: '67,00'
// example 5: number_format(1000);
// returns 5: '1,000'
// example 6: number_format(67.311, 2);
// returns 6: '67.31'
// example 7: number_format(1000.55, 1);
// returns 7: '1,000.6'
// example 8: number_format(67000, 5, ',', '.');
// returns 8: '67.000,00000'
// example 9: number_format(0.9, 0);
// returns 9: '1'
// example 10: number_format('1.20', 2);
// returns 10: '1.20'
// example 11: number_format('1.20', 4);
// returns 11: '1.2000'
// example 12: number_format('1.2000', 3);
// returns 12: '1.200'
// example 13: number_format('1 000,50', 2, '.', ' ');
// returns 13: '100 050.00'
// example 14: number_format(1e-8, 8, '.', '');
// returns 14: '0.00000001'
number = (number + '')
.replace(/[^0-9+\-Ee.]/g, '');
var n = !isFinite(+number) ? 0 : +number,
prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
s = '',
toFixedFix = function(n, prec) {
var k = Math.pow(10, prec);
return '' + (Math.round(n * k) / k)
.toFixed(prec);
};
// Fix for IE parseFloat(0.55).toFixed(0) = 0;
s = (prec ? toFixedFix(n, prec) : '' + Math.round(n))
.split('.');
if (s[0].length > 3) {
s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
}
if ((s[1] || '')
.length < prec) {
s[1] = s[1] || '';
s[1] += new Array(prec - s[1].length + 1)
.join('0');
}
return s.join(dec);
}
答案 2 :(得分:1)
使用任何插件检查此代码,我们首先用逗号制作数字。我们得到2个部分。从一个部分我们删除点并再次加入它们。
//input number
var inputNumber = "1.123.355,50";
// splitting by comma
var split = inputNumber.split(',');
//getting the first part and replacing all dots with null
var wholeNumber = split[0].replace(/\./gi, '');
//combining the splitted parts again
var formattedNumber = wholeNumber + "." + split[1];
alert(formattedNumber)
&#13;
<强>更新强> 如果你需要它作为整数,上面的代码返回一个字符串:
var formattedNumber = Number(wholeNumber + "." + split[1]);
//or
var formattedNumber = parseFloat(wholeNumber + "." + split[1]);
//or this will preserve the last 2 decimal points also
var formattedNumber = parseFloat(wholeNumber + "." + split[1]).toFixed(2);
答案 3 :(得分:0)
您可以使用replace
功能
var num = 1.123.355,50;
current = num.replace(/,/g, '.'); // Current now has '.' instead of ','
我认为Flash Thunder有更彻底的答案。
答案 4 :(得分:0)
你可以这样做:
var original = "1.123.355,50";
function formatNum(str) {
//remove the + sign if you want a string instead
return +str.replace(/\./g,'').replace(/,/g,'.')
}
console.log(formatNum(original)); //logs 1123355.5