当前位置:网站首页>数字_获取指定位数的小数
数字_获取指定位数的小数
2022-08-05 04:52:00 【乖女子@@@】
保留小数_四舍五入
Number原型链上存在一个方法toFixed
[1]语法
number.toFixed([num])number为Number类型的数据
num为可选参数:表示要保留几位小数
- 若是不传num,默认不保留小数
- 保留小数过程中会四舍五入;
[2]返回值
返回值为指定小数位数的字符串;
[3]举例说明
11.123456.toFixed() // '11'
11.123456.toFixed(3) // '11.123'
11.123456.toFixed(4) // '11.1235'
保留小数_向下取整
[1]语法
Math.floor([num])
- floor方法的作用是获取num的整数值并返回(向下取值直接截取小数点前的值)
- num为可选参数
- 不传,返回结果为NaN
- 若是传递非Number类型,该方法会默认将数据转化为Number类型再获取整数;
[2]返回值
返回Number类型的整数
[3]举例说明
// [1]获取整数
Math.floor(11.123456) // 11
// [2]获取3位小数整数
Math.floor(11.123456*1000)/1000 // 11.123
// [3]获取4位小数的整数
Math.floor(11.123456*10000)/10000 // 11.1234
保留小数_向上取整
[1]语法
Math.ceil([num])
- ceil方法的作用是获取num的整数值并返回(向上取值-只要有小数就加1)
- num为可选参数
- 不传,返回结果为NaN
- 若是传递非Number类型,该方法会默认将数据转化为Number类型再获取整数;
[2]返回值
返回Number类型的整数
[3]举例说明
// [1]获取整数
Math.ceil(11.123456) // 12
// [2]获取3位小数整数
Math.ceil(11.123456*1000)/1000 // 11.124
// [3]获取4位小数的整数
Math.ceil(11.123456*10000)/10000 // 11.1235
// [4]获取6位小数的整数
Math.ceil(11.123456*10000)/10000 // 11.123456
边栏推荐
- 延迟加载js方式async与defer区别
- [CISCN2019 华东南赛区]Web11
- The role of DataContext in WPF
- mysql数据库表什么字段类型的存储长度最大?
- 8.04 Day35-----MVC三层架构
- University Physics---Particle Kinematics
- 1007 Climb Stairs (贪心 | C思维)
- In the hot summer, teach you to use Xiaomi smart home accessories + Raspberry Pi 4 to connect to Apple HomeKit
- What is the function of industrial-grade remote wireless transmission device?
- Detailed explanation of each module of ansible
猜你喜欢

数字孪生技术在电力系统中的应用现状

AUTOCAD——标注关联

Application status of digital twin technology in power system

Some conventional routines of program development (1)

小程序_动态设置tabBar主题皮肤

特征预处理

for..in和for..of的区别

AUTOCAD - dimension association

Learning and finishing of probability theory 8: Geometric and hypergeometric distributions

大学物理---质点运动学
随机推荐
[Geek Challenge 2019]FinalSQL
C++ core programming
狗仔队:表面编辑多视点图像处理
The role of DataContext in WPF
The solution to the failure to read channel information when dedecms generates a message in the background
AUTOCAD - dimension association
Detailed explanation of Mysql's undo log
[8.2] Code Source - [Currency System] [Coins] [New Year's Questions (Data Enhanced Edition)] [Three Stages]
There are a lot of 4T hard drives remaining, prompting "No space left on device" insufficient disk space
[Surveying] Quick Summary - Excerpt from Gaoshu Gang
说说数据治理中常见的20个问题
Mysql's redo log detailed explanation
延迟加载js方式async与defer区别
数字孪生技术在电力系统中的应用现状
[BJDCTF2020] EasySearch
upload upload pictures to Tencent cloud, how to upload pictures
【8.2】代码源 - 【货币系统】【硬币】【新年的问题(数据加强版)】【三段式】
Excel画图
使用IDEA连接TDengine服务器
[SWPU2019]Web1