@writetome51/get-fixed-float

Returns number with fixed number of digits after the decimal. It also rounds cautiously to avoid cumulative errors

Usage no npm install needed!

<script type="module">
  import writetome51GetFixedFloat from 'https://cdn.skypack.dev/@writetome51/get-fixed-float';
</script>

README

getFixedFloat(
      number,
      numDigitsAfterDecimal
): string

Returns number with a decimal and exact numDigitsAfterDecimal.
If number already has more than numDigitsAfterDecimal, it will be
rounded to the last required digit after the decimal.
The algorithm prevents cumulative rounding errors.

Examples

getFixedFloat(101.975, 3); // --> '101.975'

getFixedFloat(-10.7754, 1); // --> '-10.8'

getFixedFloat(101.965, 2); // --> '101.96'

getFixedFloat(101.975, 2); // --> '101.98'

getFixedFloat(9, 2); // --> '9.00'

getFixedFloat(-100.9754, 5)); // -->  '-100.97540'

getFixedFloat(10.9754, 1); // --> '11.0'

Installation

npm i @writetome51/get-fixed-float

Loading

import {getFixedFloat}  from '@writetome51/get-fixed-float';