分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

js截取小数点后面2位

发布时间:2023-09-06 02:15责任编辑:彭小芳关键词:js
1.substrvar str = "Hello world!";document.write(str.substr(3));输出:lo world!var str = "Hello world!";document.write(str.substr(3,7));输出:lo worl2.toFixed(四舍五入)var num = new Number(12.38);document.write(num.toFixed(1));输出:12.4
3.floor(向下取整)Math.floor(0.60); ??结果:0Math.floor(0.40); ???????0Math.floor(5); ?????????5Math.floor(5.1); ???????5Math.floor(-5.1); ??????-6Math.floor(-5.9); ??????-63.ceil(向上取整)Math.ceil(0.60); ??结果:1Math.ceil(0.40); ???????1Math.ceil(5); ??????????5Math.ceil(5.1); ????????6Math.ceil(-5.1); ??????-5Math.ceil(-5.9); ??????-54.round(四舍五入)Math.round(0.60); ??结果:1Math.round(0.40); ???????0Math.round(5); ??????????5Math.round(5.1); ????????5Math.round(-5.1); ??????-5Math.round(-5.9); ??????-6使用floor配合*100/100来实现向下取整截取小数点后两位var num = 22.13758;console.log(Math.floor(num*100)/100);console.log(Math.ceil(num*100)/100);console.log(Math.round(num*100)/100);console.log(num.toFixed(2));输出:22.13 ????22.14 ????22.14 ????22.14

js截取小数点后面2位

原文地址:https://www.cnblogs.com/QW-lzm/p/9684084.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved