I'm working on a REST API that syncs client with the server's time to keep the client in sync and avoid any connection issues. I encountered this issue while dividing floats (I was doing server time (epoch) calculations):
1672866847.265 (208420 / 1000)
results in 1672867055.6850002
(which I know is IEEE 754)
mean while according to Google's calculator, it should be 1672867055.69
Is there a way to solve this issue?
CodePudding user response:
You can use toFixed(2)
to get 2 decimal points:
console.log((1672866847.265 208420 / 1000).toFixed(2));
CodePudding user response:
If you want your result to be more reliable try first multiplying
console.log(1672866847.265 (208420 / 1000))
console.log((1672866847.265 * 1000 208420) / 1000)
You can also use a library
console.log(new Big(1672866847.265).plus(new Big(208420).div(1000)).toNumber())
<script src='https://cdn.jsdelivr.net/npm/[email protected]/big.min.js'></script>
CodePudding user response:
You can use round
to avoid decimal values -
Math.round(1672867055.6850002); // ans will be: 1672867056
To get value with two decimal point, you can mupltiply it by 100 and then divid it by 100
Math.round(1672867055.6850002 * 100)/100; // ans will be: 1672867055.69