jcmag
jcmag

Reputation: 247

Angular - unit conversion

In an Angular app, I'm showing a value in "l/h" unit, but I do a conversion to store this value in "kg/s" in my database. For example, if the user enters 30 l/h, I store 30 / 0.9506 / 3600 = 0.008766393155200223

Then, when the user refreshes the page, the value displayed is not 30 but 29.999999999999993 (the result of 0.008766393155200223 * 0.9506 * 3600).

How could I avoid this conversion side-effect?

Upvotes: 0

Views: 736

Answers (1)

Javi
Javi

Reputation: 355

As JavaScript can't handle correctly floating points, in our Angular app we have been using the 'big.js' library to deal with the the decimal operations.

Installation:

npm install big.js
npm install @types/big.js

Import:

import Big from 'big.js';

Upvotes: 1

Related Questions