a JS floating point calculation problem solution:
1. Using the numberobject.tofixed (num) method
The ToFixed () method rounds numbers to a number that specifies the number of decimal digits.
2. Calculate floating-point numbers more accurately
Note: the addition of JavaScript will have an error, and it will be more obvious when the two floating-point numbers are added. This function returns a more accurate addition result. Call: Accadd (ARG1,ARG2)//return value: Arg1 plus arg2 's exact result function Accadd (arg1, arg2) {var r1, R2, M; try {r1 = (1 * arg1). toString (). Split (".") [1].length; } catch (e) {r1 = 0; try {r2 = (1 * arg2). toString (). Split (".") [1].length; } catch (e) {r2 = 0; } m = Math.pow (Ten, Math.max (R1, r2)); Return (ARG1 * m + arg2 * m)/m; }//Description: JavaScript subtraction results are error-evident when the two floating-point numbers are added together. This function returns a more accurate subtraction result. Call: Accsub (ARG1,ARG2)//return value: arg1 reduce the exact result of arg2 function accsub (arg1, arg2) {return Accadd (arg1,-ARG2); }//Description: The multiplication result of JavaScript will be error, it will be more obvious when two floating-point numbers are multiplied. This function returns a more accurate multiplication result. Call: Accmul (ARG1,ARG2)//return value: Arg1 times the exact result of arg2 function Accmul (arg1, arg2) {var m = 0, S1 = (1 * arg1). ToString ( ), S2 = (1 * arg2). toString (); try {m + = S1.split (".") [1].length; } catch (e) {} try {m + = s2.split (".") [1].length; } catch (e) {} var ss = number (S1.replace (".", "")) * Number (S2.replace (".", ""))/Math.pow (M); Return Math.Round (SS * 100)/100; }//Description: The result of the division of JavaScript will be error, it will be more obvious when the two floating-point numbers divide. This function returns a more accurate division result. Call: Accdiv (ARG1,ARG2)//return value: Arg1 divided by the exact result of arg2 function Accdiv (arg1, arg2) {var t1 = 0, t2 = 0, R1, R2; try {t1 = (1 * arg1). toString (). Split (".") [1].length; } catch (e) {} try {t2 = (1 * arg2). toString (). Split (".") [1].length; } catch (e) {} with (Math) {r1 = number ((1 * arg1). toString (). Replace (".", "")); r2 = number ((1 * arg2). toString (). Replace (".", "")); var ss = (R1/R2) * POW (ten, t2-t1); Return Math.Round (SS * 100)/100; } }
Two-Amount capitalization conversion
Amount Case Conversion function convertcurrency (currencydigits) {//Constants:var maximum_number = 99999999999.99; Predefine the radix characters and currency symbols for output:var Cn_zero = "0"; var cn_one = "one"; var cn_two = "Ii."; var cn_three = "three"; var cn_four = "the premises"; var cn_five = "WU"; var cn_six = "Lu"; var cn_seven = "Qi"; var cn_eight = "ba"; var cn_nine = "JIU"; var Cn_ten = "Pick up"; var cn_hundred = "Bai"; var cn_thousand = "thousand"; var cn_ten_thousand = "million"; var cn_hundred_million = "billion"; var cn_symbol = "RMB"; var cn_dollar = "Yuan"; var cn_ten_cent = "angle"; var cn_cent = "min"; var Cn_integer = "whole"; Variables:var Integral; represent integral part of digit number. var decimal; represent decimal part of digit number. var outputcharacters; The output result. var parts; var digits, radices, bigradices, decimals; var Zerocount; var i, p, D; var quotient, modulus; Validate Input string:currencydigits = currencydigits.tostring (); if (currencydigits = = "") {alert (" Empty input! "); Return ""; } if (Currencydigits.match (/[^,.\d]/) = null) {alert ("Invalid characters in the input string!"); Return ""; } if ((CurrencyDigits). Match (/^ ((\d{1,3} (, \d{3}) * (. ( (\d{3},) *\d{1,3}))?) | (\d+ (. \d+)?)) $/) = = null) {alert ("illegal format of digit number!"); Return ""; }//Normalize The format of input digits:currencydigits = Currencydigits.replace (/,/g, ""); Remove comma delimiters. CurrencyDigits = Currencydigits.replace (/^0+/, ""); Trim Zeros at the beginning. Assert the number is not greater than the maximum number. if (number (currencydigits) > Maximum_number) {alert ("Too large a number to convert!"); Return ""; }//Http://www.knowsky.com/Process the coversion from currency digits to characters://separate integral and decimal Parts before processing coversion:parts = Currencydigits.split ("."); if (Parts.length > 1) {integral = parts[0]; decimal = parts[1]; Cut down RedundaNT decimal digits that is after the second. decimal = Decimal.substr (0, 2); } else {integral = parts[0]; decimal = ""; }//Prepare the characters corresponding to the digits:digits = new Array (Cn_zero, Cn_one, Cn_two, Cn_three, Cn_four, Cn_five, Cn_six, Cn_seven, cn_eight,cn_nine); radices = new Array ("", Cn_ten, cn_hundred, Cn_thousand); Bigradices = new Array ("", Cn_ten_thousand, cn_hundred_million); decimals = new Array (cn_ten_cent, cn_cent); Start processing:outputcharacters = ""; Process integral part if it is larger than 0:if (number (integral) > 0) {zerocount = 0; for (i = 0; i < integral.length; i++) {p = integral.length-i-1; D = Integral.substr (i, 1); quotient = P/4; modulus = p% 4; if (d = = "0") {zerocount++; } else {if (Zerocount > 0) {outputcharacters + = digits[0]; } zerocount = 0; Outputcharacters + = Digits[number (d)] + radices[modulus]; } if (modulus = = 0 && Zerocount < 4) {outputcharacters + = bigradices[quotient]; }} outputcharacters + = Cn_dollar; }//Process decimal Part if there is:if (decimal! = "") {for (i = 0; i < decimal.length; i++) {d = decimal.substr (i, 1); if (d! = "0") {outputcharacters + = Digits[number (d)] + decimals[i]; }}}//Confirm and return the final output string:if (outputcharacters = = "") {outputcharacters = Cn_zero + C N_dollar; } if (decimal = = "") {outputcharacters + = Cn_integer; }//outputcharacters = Cn_symbol + outputcharacters; Outputcharacters = outputcharacters; return outputcharacters; }////Restore amount function Rmoney (s) {return parsefloat (S.replace (/[^\d\.-]/g, "")); }//Amount totals formatted s for the parameter to be formatted (float type), n is the number of digits left after the decimal point 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; }
Reference: http://blog.csdn.net/fairyhawk/article/details/7274990
JS floating-point calculation problem + amount capitalization conversion