Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
Javascript - Max 4 Digits after Comma

I`m looking for a way to delete the last Digits from an Input with comma if the Value is more then 4.

Example:

Value : 13,314556 should be -> 13,3145.

Is there a way to just remove everything then the last 4 digits of this number with something like this?

     let value = 13,314556
        let result= value.split(',')[1].trim();
        if(result.length > 4){
          ...
        }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

toFixed()

You can call toFixed() to format the number to a number of decimal places.

See MDN Documentation for toFixed()

Modified from the example given in the above documentation:

function myFormat(x) {
  return Number.parseFloat(x).toFixed(4);
}

// expected output: "13.3146" because of rounding
console.log(myFormat(13.314556));

// expected output: "0.0040"
console.log(myFormat(0.004));

// expected output: "123000.0000"
console.log(myFormat('1.23e+5'));

If you don't want rounding, simply be more precise and clip the end of the resulting string.

    function myFormat(x) {
      return Number.parseFloat(x).toFixed(6).replace(/\d\d$/, '');
    }

    // expected output: "13.3145", will not do rounding
    console.log(myFormat(13.314556));

If you are working with currency or do similar important calculations, consider working with a library like decimal.js.

about 4 years ago · Juan Pablo Isaza Report

0

alternative is to multiply by 10000, floor it, then divide by 10000.

let value = 13.314556;
value=Math.floor(value*10000)/10000;
console.log(value)

or if you want to control the number of digits programmatically you could use something like this:

let value = 13.314556;
value=Math.floor(value*Math.pow(10, 4))/Math.pow(10, 4);
console.log(value)

about 4 years ago · Juan Pablo Isaza Report

0

I would do it with the slice and join methods

Slice for slicing the string with the length you want

Join to re-create the number as you wish


Edit

I added check if comma is in the number and also a parameter with the desired length

function splitNumber(num, nbAfterComa) {

  const number = num.toString()
  if(!number.includes('.')){
    return number
  }
  const splitedValue = number.split('.')
  splitedValue[1] = splitedValue[1].trim().slice(0, nbAfterComa);
  return nbAfterComa > 0 ? splitedValue.join(',') : splitedValue[0]
}

console.log(splitNumber(13.314556, 4))
console.log(splitNumber(13.3, 4))
console.log(splitNumber(13.314556, 1))
console.log(splitNumber(13.314556, 0))
console.log(splitNumber(13, 8))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!