toBeCloseTo

Bun

Symbol

Matchers.toBeCloseTo

toBeCloseTo(expected: number, numDigits?: number): void

Asserts that value is close to the expected by floating point precision.

For example, the following fails because arithmetic on decimal (base 10) values often have rounding errors in limited precision binary (base 2) representation.

@param expected

the expected value

@param numDigits

the number of digits to check after the decimal point. Default is 2

expect(0.2 + 0.1).toBe(0.3); // fails

Use `toBeCloseTo` to compare floating point numbers for approximate equality.