UNPKG

to-fixed-for-real

Version:

Fix two decimal cases in currency value without worrying about wrong math rounds.

8 lines (5 loc) 274 B
module.exports = function toFixed(num, floatingPoint) { if (num.toString().includes(',')) num = num.toString().replace(',', '.'); const regex = new RegExp('^-?\\d+(?:.\\d{0,' + (floatingPoint || -1) + '})?'); return Number(num.toString().match(regex)[0]); }