lib/toFixed.js
import checkPrecision from './internal/checkPrecision';
import settings from './settings';
/**
* Implementation of toFixed() that treats floats more like decimals.
*
* Fixes binary rounding issues (eg. (0.615).toFixed(2) === '0.61') that present
* problems for accounting- and finance-related software.
*
* **Usage:**
*
* ```js
* // Native toFixed has rounding issues
* (0.615).toFixed(2);
* // => '0.61'
*
* // With accounting-js
* toFixed(0.615, 2);
* // => '0.62'
* ```
*
* @access public
* @param {Float} value - Float to be treated as a decimal number
* @param {Number} [precision=settings.precision] - Number of decimal digits to keep
* @return {String} - Given number transformed into a string with the given precission
*/
function toFixed(value, precision) {
precision = checkPrecision(precision, settings.precision);
const power = Math.pow(10, precision);
// Multiply up by precision, round accurately, then divide and use native toFixed()
return (Math.round((value + 1e-8) * power) / power).toFixed(precision);
}
export default toFixed;