0

Using Javascript, if my number is 5899, I want to insert a decimal point between the numbers to get the result of 58.99. I know (num/100).toFixed(2) would work, but just wondering if there is a way for using replace.

1 Answer 1

7

A regular expression can look ahead for 2 digits followed by the end of the string...

console.log(
  String(5899).replace(
    /(?=\d{2}$)/,
    '.'
  )
);

but .toFixed makes a lot more intuitive sense, and so should probably be preferred.

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

Comments

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.