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

176
Views
Handling rounding issue with BigNumber

I am trying to subtract two numbers with more decimals. But it is rounding up. How can I use BigNumber to make sure the numbers are not rounded and the calculation is precise.

const myObj = {
  total: 5000000,
  filled: 4999999.9999999996870756
};

const total = new BigNumber(myObj.total);
const filled = new BigNumber(myObj.filled);
const remaining = total.minus(filled);
console.log(total, filled, remaining);

if(remaining === 0.0000000003129244) console.log("remaining  calculation is correct")
else console.log("incorrect calculation")
<script src="https://cdnjs.cloudflare.com/ajax/libs/bignumber.js/8.0.2/bignumber.min.js" integrity="sha512-7UzDjRNKHpQnkh1Wf1l6i/OPINS9P2DDzTwQNX79JxfbInCXGpgI1RPb3ZD+uTP3O5X7Ke4e0+cxt2TxV7n0qQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

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

0

first you need to define string variables to not rounding it up. after that for comparing values of bigNumber it's better to use 'comparedTo' refer to documents

const myObj = {
  total: '5000000',
  filled: '4999999.9999999996870756'
};

const total = new BigNumber(myObj.total);
const filled = new BigNumber(myObj.filled);
const remaining = total.minus(filled);
console.log('total',total);
console.log('filled',filled);
console.log('remaining', remaining);

if(remaining.comparedTo(0.0000000003129244) === 0) console.log("remaining  calculation is correct")
else console.log("incorrect calculation")
<script src="https://cdnjs.cloudflare.com/ajax/libs/bignumber.js/8.0.2/bignumber.min.js" integrity="sha512-7UzDjRNKHpQnkh1Wf1l6i/OPINS9P2DDzTwQNX79JxfbInCXGpgI1RPb3ZD+uTP3O5X7Ke4e0+cxt2TxV7n0qQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

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!