UNPKG
to-fixed-for-real
Version:
latest (1.0.3)
1.0.3
1.0.2
1.0.1
1.0.0
Fix two decimal cases in currency value without worrying about wrong math rounds.
to-fixed-for-real
/
index.js
8 lines
(5 loc)
•
274 B
JavaScript
View Raw
1
2
3
4
5
6
7
8
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
]); }