js 小数[非]四舍五入
2024-10-14 16:50:38
1、四舍五入
(2.678).toFixed(2) // 2.68
2、不需要四舍五入
(parseInt(2.678*100)/100.0).toFixed(2) // 2.67
3、字节单位转换
function convertByte(bytesNum) { var unit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'BB', 'NB', 'DB'],
count = 0; if (bytesNum <= 0) {
return bytesNum;
}
if (bytesNum < 1024) {
return bytesNum + unit[0];
} while (count<unit.length) {
bytesNum = bytesNum / 1024;
if (bytesNum < 1024) {
console.log(bytesNum);
bytesNum = bytesNum.toFixed(1) == parseInt(bytesNum.toFixed(1)) ? bytesNum.toFixed(0) : bytesNum.toFixed(1);//如果有小数则保留一位小数
return bytesNum + unit[count];
}
count++;
}
}
最新文章
- angular2 service component
- C语言模块化编译介绍
- 【译】微型ORM:PetaPoco【不完整的翻译】
- Winform窗体
- SpringMVC视图
- 浙江大学 pat 题解---58
- Ajax应用-定义一套自己的Ajax框架
- 创建自己的composer包
- JavaScript学习历程03
- sort、sorted高级排序-Python3.7 And 算法<;七>;
- 【翻译】 View Frustum Culling --1 View Frustum’s Shape
- MFC命名规范
- 计算机从加电到系统(Linux)启动完成
- mysql补充(4)数据完整性
- crontab 执行脚本,报错/home/scripts/eyeMonitor.sh: line 8: node: command not found
- Visual Studio win平台 AI环境搭建
- python中的关键字符
- linux下查看doc在线帮助文件
- Web 服务器配置
- 解决ios下audio不能正常播放的问题
热门文章
- KendoUI系列:AutoComplete
- js中关于value的一个小知识点(value既是属性也是变量)
- JAVA--网络编程(UDP)
- NYOJ 1023 还是回文(DP,花最少费用形成回文串)
- poj 2777(线段树的节点更新策略)
- PHP获取图片宽度高度、大小尺寸、图片类型、用于布局的img属性
- Spark入门实战系列--6.SparkSQL(下)--Spark实战应用
- Struts2 源码分析——前言
- Android Studio导入项目非常慢的解决办法
- 图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party