Recently, the project requires the front-end to format the amount in kilobytes (that is to say, every three digits are separated by commas). google found a good method, so here we will share the effect of the following input: the JS Code is as follows: copy the code functionisString (num) {ret... syntaxHighlighter. all (); recently, the project requires us to format the amount in kilobytes (that is to say, every three digits are separated by commas) at the front end. google found a good method, so let's share it here, the result is as follows: input: JS Code: copy the code function isString (num) {return Object. prototype. toString. apply (num) = '[object String]';} function fmoney (s, n) {n = n> 0 & n <= 20? N: 2; if (isString (s) {s = s * 1;} s = parseFloat (s + "")). toFixed (n) + ""; var l = s. split (". ") [0]. split (""), 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 (""). join ("") + ". "+ r: two parameters are required. One parameter is the number to be formatted. The second parameter is the number of digits to be saved after the decimal point. The default value is two digits. if the length is greater than 3 and the maximum length is not a multiple of 3, separate them with commas. Add the number of decimal points after the split.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.