把數值改成保留小數點後兩位的格式通用方法
阿新 • • 發佈:2019-02-01
有問題可以掃碼加我微信,有償解決問題。承接小程式開發。
微信小程式開發交流qq群 173683895 、 526474645 ;
正文:
主要作用域數值顯示;請看原始碼:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <script> function returnFloat(value) { var value = Math.round(parseFloat(value) * 100) / 100; var xsd = value.toString().split("."); if(xsd.length == 1) { value = value.toString() + ".00"; return value; } else if(xsd.length > 1) { if(xsd[1].length < 2) { value = value.toString() + "0"; } return value; } } document.write(returnFloat(123)) </script> </body> </html>
方法效果:
console.log(returnFloat(52)) ==> 52.00
console.log(returnFloat(3.1415926)) ==>3.14
console.log(returnFloat(3.1)) ==> 3.10
希望能對大家有所幫助,另外歡迎大家加入微信小程式qq群(173683895)
console.log(returnFloat(3.1415926)) ==> 3.14