0

I tried using the Big library but including that code just gives me an error

Error: Cannot find module 'tap' 
...

So I tried to use Decimal instead. I did

npm install Decimal

And then added

const Decimal = require('decimal');

I followed the examples but I just get { Object (internal, as_int, ...) } as my comparison when I use

const amount = 25.12
let expectedMoney;
const Decimal = require('decimal');
...
expectedMoney = new Decimal(amount * 1.1)
expect(27.63).to.equal(expectedMoney);

Error:

 AssertionError: expected 27.63 to equal { Object (internal, as_int, ...) }
  at Context.<anonymous> (index.test.js:19:22)

I also tried:

expect(27.63).to.equal(expectedMoney.as_int.value);

But that gives

expected 27.63 to equal 27632000000000004

And I tried

expect(27.63).to.equal(expectedMoney.toFixed(5));

But that gives

TypeError: expectedMoney.toFixed is not a function
J.F.
  • 13,927
  • 9
  • 27
  • 65
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
  • if you just want to round to 2 demical places, you could use Math.round https://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-only-if-necessary – MT756 Mar 03 '20 at 15:03
  • 1
    Native JavaScript numbers are always 64-bit binary floating point values. No number will ever be equal to an object, such as an object created with that Decimal package. – Pointy Mar 03 '20 at 15:03
  • @MT756 `Math.round()` won't really help in all cases. It rounds up or down to an integer value. – Pointy Mar 03 '20 at 15:03
  • @Pointy Look at the link I posted, ```Math.round((num + Number.EPSILON) * 100) / 100``` rounds the number to 2 decimal places – MT756 Mar 03 '20 at 15:09
  • @MT756 but once you divide by 100, you may end up with stray fractional bits. That's just the nature of binary floating point. – Pointy Mar 03 '20 at 15:12
  • Yes Math.round is my answer. I will accept it. – Michael Durrant Mar 03 '20 at 15:12
  • 1
    @Pointy stray fractional bits don't matter as long as you are comparing floating point values to floating point values directly. At least not in this case where two numbers are compared directly – MT756 Mar 03 '20 at 15:15

1 Answers1

3

You don't need any library if you only want to compare with a simple "double" number.
What I used to do is cut the number at two decimal values in two ways.

I use a mathematical way using a the precision I want to get and substracting the values in this way:

var precision = 0.01;
expect(Math.abs(27.63 - expectedMoney) <= precision).to.equal(true);

The numbers will be the same if the are different in 0.001 or more. So, for example: 1.00 will be the same as 1.001 but different with 1.01.

Example here:

var precision = 0.01
//Works using 1 or 1.00
console.log(Math.abs(1 - 1.001) <= precision)
console.log(Math.abs(1 - 1.01) <= precision)

The other way is simply using toFixed() and parseFloat because toFixed returns a string.

const amount = 25.12
var expectedMoney = parseFloat((amount* 1.1).toFixed(2))
expect(27.63).to.equal(expectedMoney);

Example:

const amount = 25.12
var expectedMoney = parseFloat((amount* 1.1).toFixed(2))
console.log(27.63 == expectedMoney)
J.F.
  • 13,927
  • 9
  • 27
  • 65