更改 javascript 中的数字格式

Change Number format in javascript

我输入的号码格式如下: 1.123.355,50 JavaScript 不知道该格式,所以我需要将其传输到: 1123355.50

有什么想法吗?

来自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);
}

您可以使用replace函数

var num = 1.123.355,50;
current = num.replace(/,/g, '.'); // Current now has '.' instead of ','

我觉得闪电迅雷的回答比较彻底

你可以这样做:

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
var numberString = '1.123.355,50';
numberString = numberString
    .replace(/\./g, '')  // replace all separators
    .replace(/,/, '.');  // replace comma with dot 

var parsed = parseFloat(numberString);

在没有任何插件的情况下检查此代码,我们首先通过 comma.We 在其中获取 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)

更新: 上面的代码 returns 一个字符串,如果你需要它作为整数,你可以这样做:

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);