1

I have the following How do I convert OrderID to thousands separator i.e., 100,000?? I'm doing it this way but it's not working:

var thousandseparator = [
"QRDER_QTY", "EXEC_QTY", "AVG_PX", "NOTIONAL_USD", "LIMIT_PX", "LIQ_CONSUMPTION"
];

for(var i = 0; i < json.length; i++) {
    var obj = json[i];
    for(key in obj) {
        if(thousandseparator.indexOf(key) != -1) {
        obj[key] = Number(obj[key].toString().toLocaleString());
      }

    }
}

Also, I'm converting each element to integer like this:

var jsondata = document.getElementById("jsonArray").value;
    var json = JSON.parse(jsondata);

var parsedData = json.map(function(obj) {
    return Object.keys(obj).reduce(function(memo, key) {
        var value = obj[key];
        //console.log(value);
        memo[key] = isNumeric(value) ? Number(value) : value;
        //localStorage.setItem("storeditem", value);

        return memo;
    }, {})
});

But on doing so, the decimals like 2.40 are converted to 2.4. How do I make it 2.40 only??

shek
  • 247
  • 2
  • 7
  • 15
  • Why are you converting the values to numbers if you want to format them? There are many questions about formatting numbers on SO, e.g. [How to print a number with commas as thousands separators in JavaScript](http://stackoverflow.com/q/2901102/218196) – Felix Kling Sep 29 '16 at 19:58
  • @FelixKling Becuase I need to also add sorting functionality on them later – shek Sep 29 '16 at 20:02
  • 3
    You can't have both though. You can either have a number value or have a string value representing a formatted number. – Felix Kling Sep 29 '16 at 20:03

2 Answers2

4

Here is how to use toLocaleString() (I've cut down your sample to just the essentials)

var obj = {
    "ROOT_ORDER_ID": "735422197553491",
    "AVG_PX": "93.6586",
    "NOTIONAL_USD": "477940",
    "LIQ_CONSUMPTION": "15.21",
    "EXEC_QTY": "5103",
    "LIMIT_PX": "93.6100",
    "ORDER_QTY": "5103"
}

var thousandseparator = [
"ORDER_QTY", "EXEC_QTY", "AVG_PX", "NOTIONAL_USD", "LIMIT_PX", "LIQ_CONSUMPTION"
];

for(key in obj) {
  if(thousandseparator.indexOf(key) != -1) {
    obj[key] = Number(obj[key]).toLocaleString();
  }
}

console.log(obj)

You want to use it on a number, hence you first want to do Number(obj[key]) and then call .toLocaleString() on the result, however your code was doing the operation in reverse.

As for your second question - you cannot have a formatted numeric value. JavaScript shows numbers in the most "convenient" way possible, although that's not the best way. In general, any extra zeroes in the fractional part will be hidden and numbers that are too long will be shortened to scientific notation (for example 1e24).

If you want to display numeric data formatted, it would have to be done as a string. If you want to show an exact number of decimal places, then use Number#toFixed() which returns a string:

var pi = 3.141592;
console.log(pi, typeof pi);

var shortPi = pi.toFixed(2);
console.log(shortPi, typeof shortPi);

var money = 3.50;
console.log(money, typeof money);

var formattedMoney = money.toFixed(2);
console.log(formattedMoney, typeof formattedMoney);
VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • Is it not possible to return a formatted number to 2 decimals as a numbers or float? – shek Sep 29 '16 at 20:59
  • @shek no, as I said, JS automatically determines how numeric values are to be displayed. As a rule of thumb, if you want to display something, you probably want it to be a string, if you want to do mathematical operations, it needs to be a number. And if both are needed, then you will end up having two different representations (even one type keeps being converted to the other). – VLAZ Sep 29 '16 at 21:02
1

Try replacing:

obj[key] = Number(obj[key].toString().toLocaleString());

with:

obj[key] = Number(obj[key]).toLocaleString();

Shawn Northrop
  • 5,826
  • 6
  • 42
  • 80