js实现对一个数格式化,先保留两位小数(四舍五入),
编辑: admin 2017-27-02
-
<!DOCTYPE HTML>
-
4
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>menu</title>
<style type="text/css">
</style>
<script type="text/javascript">
\x09function format (num, fixed)
{
\x09 return num.toFixed (fixed).replace (/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
}
console.log (format (1.566, 2));
console.info (format (1.56, 2));
console.error (format (1234.566, 2));
console.debug (format (1234567.56, 2));
console.warn (format (0, 2));
</script>
</head>
<body>
</body>
</html>