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

421
Views
Javascript how to check if the Quotient is an Integer

Example:

1.15 / 0.01 = 114.99999999999999, is an Integer in my case
9.96 / 0.01 = 996.0000000000001, is an Integer in my case
15.121 / 0.01 = 1512.1, Not an Integer

Number.isInteger() is not accurate in cases: 1.15 / 0.01 and 9.96 / 0.01

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I'd suggest using a dedicated library for this, such as decimal.js.

Decimal objects have a useful method isInteger(), that can be invoked after the division is complete.

You can set the precision required, though I believe the default should suffice (20, see precision)

function isQuotientInteger(dividend, divisor) {
    return new Decimal(dividend).dividedBy(new Decimal(divisor)).isInteger();
}

let testInputs = [ { dividend: 1.15, divisor: 0.01 }, { dividend: 9.96, divisor: 0.01 }, { dividend: 15.121, divisor: 0.01 } , { dividend: 3.14159, divisor: 0.00001 }];

formatRow('Dividend', 'Divisor', 'Quotient is integer')
for(let testInput of testInputs) {
    formatRow(testInput.dividend, testInput.divisor, isQuotientInteger(testInput.dividend, testInput.divisor))
}

function formatRow(...row) {
    console.log(...row.map(f => (f + '').padEnd(10)))
}
   
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/decimal.js/10.3.1/decimal.min.js" integrity="sha512-Ou4M+sSU8oa+mE3juYqR3JmW633MUpMhe1cd+IusOtfjkMo8I3zXs4fRmjmCFqpRg5RK/geqoXBY8XRwFY2Rsg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

about 4 years ago · Juan Pablo Isaza Report

0

Borrowing from this thread:

Rounding to the nearest hundredth of a decimal in JavaScript

let ex1=1.15 / 0.01
let ex2=9.96 / 0.01
let ex3=15.121 / 0.01
const desired_place=3

function round(num, places) {
    //round # to however many places you need
    let multiplier = Math.pow(10, places);
    let rounded= Math.round(num * multiplier) / multiplier;
    return Number.isInteger(rounded)? true : false
}

console.log(round(ex1,desired_place))  //true
console.log(round(ex2,desired_place))  //true
console.log(round(ex3,desired_place))  /false
about 4 years ago · Juan Pablo Isaza Report

0

In Javascript all numbers are internally represented as floating point numbers, which can in many cases lead to visible or invisible rounding issues, when mathematical operations are executed. It is unfortunate that Number.isInteger() produces an unexpected result in the cases you list, where we humans can clearly "see" that the result of the operation must be an integer value. Here is a practical solution to find out if a quotient is an integer with a given precision:

First you should use Number.toFixed() to get a number rounded to your required precision, and then apply Number.toInteger().

function isInteger(arg) {
  const precision = 12; // for example
  return Number.isInteger(Number(arg.toFixed(precision)));
}

console.log(isInteger(1.15 / 0.01));
console.log(isInteger(9.96 / 0.01));
console.log(isInteger(15.121 / 0.01));

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!