做BS的兄弟都知道js,我们搞Bs就少不了要和js打些交道。不会一定js还真是不行,有些的地方就是离不开js。在form表单中,有时候会要保留小数。精确小数点到多少位。那么下面的一些方法就可以帮我们做到。 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>floatDecimal.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <script type="text/javascript"> //保留两位小数 //功能:将浮点数四舍五入,取小数点后2位 function toDecimal(x) { var f = parseFloat(x); if (isNaN(f)) { return; } f = Math.round(x*100)/100; return f; } //制保留2位小数,如:2,会在2后面补上00.即2.00 function toDecimal2(x) { var f = parseFloat(x); if (isNaN(f)) { return false; } var f = Math.round(x*100)/100; var s = f.toString(); var rs = s.indexOf(\'.\'); if (rs < 0) { rs = s.length; s += \'.\'; } while (s.length <= rs + 2) { s += \'0\'; } return s; } function fomatFloat(src,pos){ return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos); } //四舍五入 alert("保留2位小数:" + toDecimal(3.14159267)); alert("强制保留2位小数:" + toDecimal2(3.14159267)); alert("保留2位小数:" + toDecimal(3.14559267)); alert("强制保留2位小数:" + toDecimal2(3.15159267)); alert("保留2位小数:" + fomatFloat(3.14559267, 2)); alert("保留1位小数:" + fomatFloat(3.15159267, 1)); //五舍六入 alert("保留2位小数:" + 1000.003.toFixed(2)); alert("保留1位小数:" + 1000.08.toFixed(1)); alert("保留1位小数:" + 1000.04.toFixed(1)); alert("保留1位小数:" + 1000.05.toFixed(1)); //科学计数 alert(3.1415.toExponential(2)); alert(3.1455.toExponential(2)); alert(3.1445.toExponential(2)); alert(3.1465.toExponential(2)); alert(3.1665.toExponential(1)); //精确到n位,不含n位 alert("精确到小数点第2位" + 3.1415.toPrecision(2)); alert("精确到小数点第3位" + 3.1465.toPrecision(3)); alert("精确到小数点第2位" + 3.1415.toPrecision(2)); alert("精确到小数点第2位" + 3.1455.toPrecision(2)); alert("精确到小数点第5位" + 3.141592679287.toPrecision(5)); </script> </head> <body> This is my HTML page. <br> </body> </html>
#方法1:print("%.1f"%0.13333)#方法2print("{:.1f}".format(0.13333))#方法3round(0.13333,1)
/***对数字字符串不四舍五入处理**@paramstr处理参数*@paramscale保留小数位数*@return返回值*/publicclassRoundNoOfUtil{publicstaticStringRoundNoOf(Stri…
1.$(window).height()获取屏幕高度2.$("#chartbottomdiv").width()某个控件的属性用"."3.//保留两位小数functiontwotoFixed(value){if(value==null||v…
php保留两位小数并且四舍五入$num=5566.56831;echosprintf("%.2f",$num);php保留两位小数并且不四舍五入$num=5566.56831;echosprintf("%.2f",substr(sprint…
结合一下网上的例子$num=10.4567;//第一种:利用round()对浮点数进行四舍五入但是这个如果没有两位小数也不会"两位精度"echoround($num,2);//10.46echoround('1.1',2);//1.1//第…
可以看我这个日志将计算方法的http://www.cnblogs.com/shangbolei/p/4441946.html用PHP写的functionfloat_bin($shang){//保留6位小数的浮点型保留更多位的更改$x=dec…
第一种方法:javascript实现保留两位小数一位自动补零代码实例:第一种方法介绍一下如何实现对数字保留两位小数效果,如果数字的原本小数位数不到两位,那么缺少的就自动补零,这个也是为了统一的效果,先看代码实例:?1234567891011…
以下我们将为大家介绍JavaScript保留两位小数的实现方法:四舍五入以下处理结果会四舍五入:?12varnum=2.446242342;num=num.toFixed(2);//输出结果为2.45不四舍五入以下处理结果不会四舍五入:第一…