javascript如何去掉小数点后两位
导读:本文共866.5字符,通常情况下阅读需要3分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 方法:1、使用“数值字符串对象.substring(0,数值字符串对象.indexOf(".")+3)”语句;2、使用“Math.round(数值对象*100)/100”语句;3、使用“数值对象.toFixed(2)”语句。本教程... ...
音频解说
目录
(为您整理了一些要点),点击可以直达。
方法:1、使用“数值字符串对象.substring(0,数值字符串对象.indexOf(".")+3)”语句;2、使用“Math.round(数值对象*100)/100”语句;3、使用“数值对象.toFixed(2)”语句。
本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。
javascript如何去掉小数点后两位?例22.123456取成22.12要如何做?
1、通过substring截取
functiongetnum(){varnum=22.123456;vars=num.toString();varresult=s.substring(0,s.indexOf(".")+3);alert(result);}getnum();
2、正则表达式
functiongetnum(){varnum=22.123456;varstr=num.toString();varaNew;varre=/([0-9]+\.[0-9]{2})[0-9]*/;aNew=str.replace(re,"$1");alert(aNew);}getnum();
3、数据类型保留上
functiongetnum(){varnum=22.123456;alert(Math.round(num*100)/100);}getnum();
4、toFixed方法
varnum=22.123456;alert(num.toFixed(2));
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:
javascript如何去掉小数点后两位的详细内容,希望对您有所帮助,信息来源于网络。