JS format numeric amounts with commas separated by two decimal places

  • 2020-03-26 21:28:18
  • OfStack

Such as:
12345 is formatted as 12,345.00
12345.6 is formatted as 12,345.60
12345.67 is formatted as 12,345.67
I'm just going to leave two decimal places.
I came back and wrote a formatting function. Can control the decimal number, automatic rounding. The code is as follows:
 
function fmoney(s, n) { 
n = n > 0 && n <= 20 ? n : 2; 
s = parseFloat((s + "").replace(/[^d.-]/g, "")).toFixed(n) + ""; 
var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1]; 
t = ""; 
for (i = 0; i < l.length; i++) { 
t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : ""); 
} 
return t.split("").reverse().join("") + "." + r; 
} 

Call: fmoney("12345.675910", 3), return 12,345.676
Reducing function:
 
function rmoney(s) { 
return parseFloat(s.replace(/[^d.-]/g, "")); 
} 

Example (you can save the code as an HTML file to run the view) :
 
<SCRIPT> 
function fmoney(s, n) { 
n = n > 0 && n <= 20 ? n : 2; 
s = parseFloat((s + "").replace(/[^d.-]/g, "")).toFixed(n) + ""; 
var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1]; 
t = ""; 
for (i = 0; i < l.length; i++) { 
t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : ""); 
} 
return t.split("").reverse().join("") + "." + r; 
} 
function rmoney(s) { 
return parseFloat(s.replace(/[^d.-]/g, "")); 
} 
function g(id) { 
return document.getElementById(id); 
} 
window.onload = function() { 
var num, txt = g("txt"), txt2 = g("txt2"), btn = g("btn"), btn2 = g("btn2"), span = g("span"); 
btn.onclick = function() { 
num = parseInt(g("num").value); 
txt.value = fmoney(txt.value, num); 
txt2.value = fmoney(txt2.value, num); 
}; 
btn2.onclick = function() { 
num = parseInt(g("num").value); 
span.innerHTML = "=" 
+ fmoney(rmoney(txt.value) + rmoney(txt2.value), num); 
}; 
}; 
</SCRIPT> 
 Decimal number:  
<select id="num"> 
<option value="2">2</option> 
<option value="3">3</option> 
<option value="4">4</option> 
<option value="5">5</option> 
</select> 
<input type="text" id="txt" value="12345.675910"> + 
<input type="text" id="txt2" value="1223"> <span id="span"></span> 
<br> 
<input type="button" id="btn" value=" formatting "> 
<input type="button" id="btn2" value=" add "> 

The attached:
 
 
function formatMoney(s, type) { 
if (/[^0-9.]/.test(s)) 
return "0"; 
if (s == null || s == "") 
return "0"; 
s = s.toString().replace(/^(d*)$/, "$1."); 
s = (s + "00").replace(/(d*.dd)d*/, "$1"); 
s = s.replace(".", ","); 
var re = /(d)(d{3},)/; 
while (re.test(s)) 
s = s.replace(re, "$1,$2"); 
s = s.replace(/,(dd)$/, ".$1"); 
if (type == 0) {//No decimal place (with decimal place by default)
var a = s.split("."); 
if (a[1] == "00") { 
s = a[0]; 
} 
} 
return s; 
} 
 
function DateAdd(interval, number, date) { 
if (date == null) 
return ""; 
switch (interval) { 
case "day": 
date = new Date(date); 
date = date.valueOf(); 
date += number * 24 * 60 * 60 * 1000; 
date = new Date(date); 
return date; 
break; 
default: 
return ""; 
break; 
} 
} 

Related articles: