Reputation: 63
I have a number string which consists of: "969239274411254159183486"
I need this to format into this format (with either Javascript or Jquery): "969,239" so that it is in 'thousands'.
I tried parseInt without success, what do I need to use to format it into the right format?
Thanks in advance.
EDIT
Just copying the comment here posted by the person asking the question in order to avoid confusion.
Input is the large string, output to be expected is 969,239. And yes, always the first 6.
Upvotes: 0
Views: 635
Reputation: 4296
const str = "969239274411254159183486";
function getDecimal( input , numBeforeComma , numAfterComma){
const res = str.slice(0, numBeforeComma) + "," + str.slice(numBeforeComma, numAfterComma+numBeforeComma);
return res;
}
console.log(getDecimal(str,3,3));
console.log(getDecimal(str,3,2));
I made a function where you can specify the amount of numbers before the comma. And the amount after. It returns it for you. you can parseInt the result.
Upvotes: 0
Reputation: 73221
Just take the first 6 digits, make it a number and use toLocaleString
on it
const s = "969239274411254159183486";
console.log(Number(s.substr(0,6)).toLocaleString('en'));
Upvotes: 3
Reputation: 44087
If you want the first six digits formatted with a comma, use slice
.
const str = "969239274411254159183486";
const res = str.slice(0, 3) + "," + str.slice(3, 6);
console.log(res);
Upvotes: 0