7

Below is my JavaScript:

var s = new Number(123456789);
alert(s.toLocaleString("en-US"));

this gives result 123,456,789 in chrome. But IE 8 shows 123,456,789.00. Is there any workaround to restrict the addition of ".00" in IE?


FYI: I have already checked This which gives problem in Chrome and have searched around google with no use.

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Naveen
  • 1,496
  • 1
  • 15
  • 24
  • 3
    According to the [*language specification*](http://ecma-international.org/ecma-262/5.1/#sec-15.7.4.3): "This function is implementation-dependent". In other words, there is no control over its format and it might be different for each implementation (e.g. from one browser to the next). For some it might be "123.456.789,00" depending on browser settings so you can't simply trim from the "." to the end. – RobG Oct 22 '14 at 10:07

2 Answers2

2

// Following @RobG's comment I have altered and simplified to find any character that might be used as a decimal point (unless it's the first character)

var s = new Number(123456789);
var s1 = s.toLocaleString();
var p = new Number(Math.floor(s) + 0.1);    // similar value but decimal
var p1 = p.toLocaleString();
var index;
var point;
for (index=p1.length-1; index>0; index--) { // find decimal point in dummy
    point = p1.charAt(index);
    if (point < '0' || point > '9')
        break;
    }
if (index > 0) {
    index = s1.lastIndexOf(point);          // find last point in string
    if (index > 0)
        s1 = s1.slice(0, index);            // truncate decimal part
    }
alert(s1);
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • 4
    [*Number.prototype.toLocaleString*](http://ecma-international.org/ecma-262/5.1/#sec-15.7.4.3) doesn't take any arguments so relying on that isn't a good strategy. – RobG Oct 22 '14 at 11:37
2

You can test for the decimal separator and remove it and everything thereafter:

// Work out whether decimal separator is . or , for localised numbers
function getDecimalSeparator() {
  return /\./.test((1.1).toLocaleString())? '.' : ',';
}

// Round n to an integer and present
function myToLocaleInteger(n) {
  var re = new RegExp( '\\' + getDecimalSeparator() + '\\d+$');
  return Math.round(n).toLocaleString().replace(re,'');
}

// Test with a number that has decimal places
var n = 12345.99

console.log(n.toLocaleString() + ' : ' + myToLocaleInteger(n)); // 12,345.99 : 12,346

You'll need to change system settings to test thoroughly.

Edit

If you want to change the built–in toLocaleString, try:

// Only modify if toLocaleString adds decimal places
if (/\D/.test((1).toLocaleString())) {

  Number.prototype.toLocaleString = (function() {

    // Store built-in toLocaleString
    var _toLocale = Number.prototype.toLocaleString;

    // Work out the decimal separator
    var _sep = /\./.test((1.1).toLocaleString())? '.' : ',';

    // Regular expression to trim decimal places
    var re = new RegExp( '\\' + _sep + '\\d+$');

    return function() {

      // If number is an integer, call built–in function and trim decimal places
      // if they're added
      if (parseInt(this) == this) {
        return _toLocale.call(this).replace(re,'');
      }

      // Otherwise, just convert to locale
      return _toLocale.call(this);
    }
  }());
}

This will modify the built–in toLocaleString only if it adds decimal places to integers.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • Is there any workaround for "toLocaleString()" itself? My actual need is to format the number. So i thought of using this function, it turned out to be problem in IE. And it should work in IE 8 onwards. – Naveen Oct 22 '14 at 11:57
  • It's a built–in method, you can replace it. But that will replace it for everyone. – RobG Oct 22 '14 at 11:59
  • Yeah,I think i will write my own something like this http://stackoverflow.com/questions/3883342/add-commas-to-a-number-in-jquery – Naveen Oct 22 '14 at 12:01
  • But that completely ignores the *locale* and assumes it uses "," as the thousands separator. You could test for the local decimal separator and infer the thousands separator from that. – RobG Oct 22 '14 at 12:24