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

192
Views
How to get the smallest fractional number correct to x decimal places?

Given some number of decimal places, how can I get the smallest number that matches those decimals?

E.g.

For 3 decimal places, it would be 0.001.

For 5 decimal places, it would be 0.00001.

For 0 decimal places, it would be 0

...and so on

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Math.pow can help:

console.log(10 ** -1); // 0.1
console.log(10 ** -2); // 0.01
console.log(10 ** -3); // 0.001

So if n is the amount of decimals you want, do Math.pow(10, -n)

copy-paste-ready function:

function smallestFractionalNumberWithDecimals(n) {
  if (n <= 0) {
    return 0;
  }
  return Math.pow(10, -n);
}

only exception is for n=0 because Math.pow(10, 0) is 1

about 4 years ago · Santiago Trujillo Report

0

The below logic can be one way to do what you want to generate:

function generate(n) {
    if (n >= 0) {
        return (n === 0) ? 0 : 1 / Math.pow(10, n);
    }
    else {
        return -1;
    }
}

console.log(generate(3));
console.log(generate(5));
console.log(generate(0));

about 4 years ago · Santiago Trujillo 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!