- For example:
- 12345 formatted as 12,345.00
- 12345.6 formatted as 12,345.60
- 12345.67 formatted as 12,345.67
- Leave only two decimal places.
- When I came back, I wrote a format function. You can control the number of decimal digits and auto-rounding.
<SCRIPT>functionFmoney (s, n) {n= n > 0 && n <= 20? N:2; S= Parsefloat ((s + ""). Replace (/[^\d\.-]/g, ""). ToFixed (n) + ""; varL = 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? "," : ""); } returnT.split (""). Reverse (). Join ("") + "." +R; } functionRmoney (s) {returnParsefloat (S.replace (/[^\d\.-]/g, "")); } functiong (ID) {returndocument.getElementById (ID); } window.onload=function() { varnum, 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>
Front desk:
Number of decimal places:<SelectID= "num"> <optionvalue= "2">2</option> <optionvalue= "3">3</option> <optionvalue= "4">4</option> <optionvalue= "5">5</option> </Select> <inputtype= "text"ID= "txt"value= "12345.675910"> + <inputtype= "text"ID= "Txt2"value= "1223"> <spanID= "span"></span> <BR> <inputtype= "button"ID= "BTN"value= "Formatted"> <inputtype= "button"ID= "BTN2"value= "Add">
JS number, the amount is separated by commas. Digital formatting