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??

3
  • 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 Commented Sep 29, 2016 at 19:58
  • @FelixKling Becuase I need to also add sorting functionality on them later Commented Sep 29, 2016 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. Commented Sep 29, 2016 at 20:03

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Is it not possible to return a formatted number to 2 decimals as a numbers or float?
@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).
1

Try replacing:

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

with:

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

4 Comments

What's wrong with what I've done?? Because it's not working
Why would that make a difference?
Is this not returning as a Number? Because I want to sort based on it as well.
Didn't mean to change Number to parseInt, I think the issue is that you are calling toLocaleSting on a String rather than a Number, see updated answer.
0

Using regex:

var json = {
    "number_1": "1234578",
    "number_2": "123456",
    "number_3": "12345",
    "number_4": "1234",
    "number_5": "123",
    "number_6": "12",
    "number_7": "1"
}

for( var key in json ) {
    var key_with_separator = json[key].toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,');
    console.log( key_with_separator );
}

Source: http://www.kompx.com/en/add-thousands-separator-into-numbers-javascript.htm

1 Comment

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.