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.

6
  • Use stackoverflow.com/questions/149055/… Commented Jun 14, 2019 at 9:19
  • 3
    @yajiv didn't he already wrote that? "969239274411254159183486" and "969,239" Commented Jun 14, 2019 at 9: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. Commented Jun 14, 2019 at 9: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 Commented Jun 14, 2019 at 9: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? Commented Jun 14, 2019 at 9:23

3 Answers 3

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

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

Comments

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

Comments

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.

2 Comments

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

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.