0

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.

ArslanIqbal
  • 569
  • 1
  • 6
  • 19
Aids Jung
  • 63
  • 1
  • 7
  • Use https://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-currency-string-in-javascript – Mohammadreza Yektamaram Jun 14 '19 at 09:19
  • 3
    @yajiv didn't he already wrote that? `"969239274411254159183486"` and `"969,239"` – Carsten Løvbo Andersen Jun 14 '19 at 09:21
  • @RoryMcCrossan it is a result from a API call, as it is 18 decimals it is a long string. Now I gotta parse it back. – Aids Jung Jun 14 '19 at 09:23
  • 1
    Assuming the number is always the same magnitude: `Math.floor(parseInt('969239274411254159183486') / 1000000000000000000)` I'd suggest working with the value in a more succinct manner, though – Rory McCrossan Jun 14 '19 at 09:23
  • Did you mean for the whole number to get the thousands separator throughout, and just truncated the example output in your question because you didn't want to type them all? If so, as you can see from the above - it's causing more questions to be asked. You need to be very clear as to what you're expecting to have as input and output. If it is just those 6 digits, what are the rules for picking those 6? Is it always the first 6 etc? – James Thorpe Jun 14 '19 at 09:23
  • Input is the large string, output to be expected is 969,239. And yes, always the first 6. @JamesThorpe – Aids Jung Jun 14 '19 at 09:29

3 Answers3

3

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'));
baao
  • 71,625
  • 17
  • 143
  • 203
0

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);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

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.

Kevin.a
  • 4,094
  • 8
  • 46
  • 82
  • If you click `<>` in editor could put this in stack snippet so it can be run here in page. Strange output format you are generating though – charlietfl Jun 14 '19 at 09:40
  • @charlietfl thank you for the tip. However i dont see the <> in the editor. :/ Also how is the output strange. It returns what the OP wanted. I just put a different example so he can see it works with any number. Its not static it can be anything you like. – Kevin.a Jun 14 '19 at 09:43