Home  >  Article  >  Web Front-end  >  Javascript set amount style conversion to retain two decimal places sample code_javascript skills

Javascript set amount style conversion to retain two decimal places sample code_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:10:441473browse
Copy code The code is as follows:

//Amount format conversion
function parsePrice(s) {
var n = 2 //Set the number of decimal places to retain
s = parseFloat((s "").replace(/[^d.-]/g, "")).toFixed(n) "" ;
var l = s.split(".")[0].split("").reverse();
var r = s.split(".")[1];
var t = "";
for (i = 0; i < l.length; i ) {
t = l[i];
}
return '¥' t.split( "").reverse().join("") "." r;
}

Use:
Copy code The code is as follows:

var m=10;
parsePrice(m);//Output: ¥10.00

var m=12.2
parsePrice(m);//Output: ¥12.20

var m=12.31
parsePrice(m);//Output: ¥12.31

Other information: In the browser according to the URL settings, reopen a tab:
Copy the code The code is as follows:

window.open("http://www.allinpay.com");
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn