JavaScript小数取整

2,778 阅读1分钟

取整:

向下取整Math.floor(),向上取整Math.ceil(),四舍五入Math.round()),保留有效数位n.toFixed(),产生大于等于0小于1的随机数Math.random()


功能 函数 示例
整型 向下取整 Math.floor() Math.floor(1.1)==>1
向上取整 Math.ceil() Math.ceil(12.1) ==>13
四舍五入 Math.round() Math.round(1.1)==>1
Math.round(1.6)==>2
浮点型 保留有效数位 n.toFixed() 1.442.toFixed(2)==>1.44
1.446.toFixed(2)==>1.45
随机数 Math.random() Math.random()==>0.38078270980122775