Financial tasks always have high requirements on performance, usability, and accuracy, including some small details.
For example, when the amount needs to be displayed, it is converted to a sub-digit with two digits after the decimal point. When editing, You need to format and remove the commas. I found a code segment online, but there was a problem when I edited it again. I modified the Code as follows:
- Function outputmoney (number ){
- Number = number. Replace (//,/g ,"");
- If (isnan (number) | Number = "") Return "";
- Number = math. Round (Number * 100)/100;
- If (number <0)
- Return '-' + outputdollars (math. Floor (math. Abs (number)-0) + '') + outputcents (math. Abs (number)-0 );
- Else
- Return outputdollars (math. Floor (number-0) + '') + outputcents (number-0 );
- }
- Function outputdollars (number)
- {
- If (number. Length <= 3)
- Return (number = ''? '0': number );
- Else
- {
- VaR mod = number. Length % 3;
- VaR output = (mod = 0? '': (Number. substring (0, MoD )));
- For (I = 0; I <math. Floor (number. Length/3); I ++)
- {
- If (mod = 0) & (I = 0 ))
- Output + = number. substring (mod + 3 * I, MOD + 3 * I + 3 );
- Else
- Output + = ',' + number. substring (mod + 3 * I, MOD + 3 * I + 3 );
- }
- Return (output );
- }
- }
- Function outputcents (amount)
- {
- Amount = math. Round (amount)-math. Floor (amount) * 100 );
- Return (amount <10? '. 0' + amount:'. '+ amount );
- }
Usage:
<Input name = "trans_amount" type = "text" id = "trans_amount" size = "15" onblur = "This. value = outputmoney (this. Value);"/>