1

I have input number in this format: 1.123.355,50 and JavaScript does not know that format so I need to transfer it to: 1123355.50

Any ideas?

1
  • Where did you get that number from? Maybe if you got it from some library, it has an option to output numbers in a JavaScript-friendly format? Commented Feb 9, 2015 at 13:06

5 Answers 5

2
var numberString = '1.123.355,50';
numberString = numberString
    .replace(/\./g, '')  // replace all separators
    .replace(/,/, '.');  // replace comma with dot 

var parsed = parseFloat(numberString);
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

Check this code with out any plugin, we are first braking the number by comma.We get 2 parts in this. From one part we remove the dots and join them again.

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

UPDATE: Above code returns a string if you need it as integer you can do:

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

6 Comments

Keep in mind that that way you get a string, not a number, if that is important for you.
This will convert it back to integer: alert(Number(formattedNumber))
alert doesn't care if it is integer or not. Other code might care.
Means you can assign it to any variable var formattedNumber = Number(wholeNumber + "." + split[1]); //or var formattedNumber = parseFloat(wholeNumber + "." + split[1]);
That's better. That should probably be a part of your answer, since OP didn't say whether he needs a string or a number.
|
0

You can use the replace function

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

I think Flash Thunder has the more thorough answer.

Comments

0

You could do something like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.