javascript - Number.toLocaleString() with custom separators -
i need format number in javascript separators may defined @ runtime.
the combination of thousand , decimal separator may not match specific locale.
is there way provide thousand , decimal separator in javascript tolocalestring()
? or use numberformat
explicitly values define?
i see examples using locale codes, , using other values ( https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/number/tolocalestring ) these don't cover use case.
one option format string known separators, , find/replace unknown separators.
function formatnum(num, separator, fraction) { var str = num.tolocalestring('en-us'); str = str.replace(/\./, fraction); str = str.replace(/,/g, separator); return str; } formatnum(12342.2, "a", "x"); //12a342x2
Comments
Post a Comment