js 向上取整、向下取整、四舍五入

CreateTime--2018年4月14日11:31:21

Author:Marydon

// 1.只保留整数部分(丢弃小数部分)
parseInt(5.1234);//
// 2.向下取整(<= 该数值的最大整数)和parseInt()一样
Math.floor(5.1234);// 5
// 3.向上取整(有小数,整数就+1)
Math.ceil(5.1234); // 4.四舍五入(小数部分)
Math.round(5.1234);//
Math.round(5.6789);//
// 5.绝对值
Math.abs(-1);//
// 6.返回两者中的较大值
Math.max(1,2);//
// 7.返回两者中的较小值
Math.min(1,2);//
// 随机数(0-1)
Math.random();

关于Math.floor()与parseInt()

它们两个都是只保留整数部分,但是在转换时可能会出现不精确的情况:

临界点:

  当有16位小数,且最后一位小数为5时,取的值是该数值的最大整数;

  Math.floor(5.9999999999999995);// 5

  当有16位小数,且最后一位小数为6时,取的值是该数值的最大整数+1。

  Math.floor(5.9999999999999996);// 6

相关推荐:

   类似文章

 

js 向上取整、向下取整、四舍五入的更多相关文章

  1. C#采用的是“四舍六入五成双”、上取整、下取整

    c# 四舍五入.上取整.下取整 Posted on 2010-07-28 12:54 碧水寒潭 阅读(57826) 评论(4) 编辑 收藏 在处理一些数据时,我们希望能用“四舍五入”法实现,但是C#采 ...

  2. c# 四舍五入、上取整、下取整

    在处理一些数据时,我们希望能用“四舍五入”法实现,但是C#采用的是“四舍六入五成双”的方法,如下面的例子,就是用“四舍六入五成双”得到的结果: double d1 = Math.Round(1.25, ...

  3. c#中取整,向上取,向下取

    Math.Ceiling()向上取整, Math.Floor()向下取整 示例: d = 4.56789 Math.Ceiling(Convert.ToDecimal(d)).ToString();M ...

  4. SQLSERVER 数值 四舍五入取整 向上取整 向下取整

    [四舍五入取整截取] select round(54.56,0) [向下取整截取] SELECT floor(54.56) [向上取整截取]  SELECT   ceiling(13.15)

  5. python中的向上取整向下取整以及四舍五入的方法

    import math #向上取整print "math.ceil---"print "math.ceil(2.3) => ", math.ceil(2. ...

  6. Python向上取整,向下取整以及四舍五入函数

    import math f = 11.2 print math.ceil(f) #向上取整 print math.floor(f) #向下取整 print round(f) #四舍五入 #这三个函数的 ...

  7. c# 三种取整方法 向上取整 向下取整 四舍五入

    Math.Round:四舍六入五取整 Math.Ceiling:向上取整,只要有小数都加1 Math.Floor:向下取整,总是舍去小数

  8. java向上取整向下取整

    向上取整用Math.ceil(double a) 向下取整用Math.floor(double a) 举例: public static void main(String[] args) throws ...

  9. python向上取整 向下取整

    向上取整 ceil() 函数返回数字的向上取整整数,就是返回大于等于变量的最近的整数. ceil()是不能直接访问的,需要导入 math 模块. import math math.ceil( x ) ...

  10. sql 向上取整 向下取整 四舍五入的实例;

    SELECT CEILING(23.5/4)'向上取整' ---6 :SELECT FLOOR(23.5/4)'向下取整' ---5 :SELECT ROUND(23.5/4,1)'四舍五入' --5 ...

随机推荐

  1. Codeforces Round #245 (Div. 2) C. Xor-tree DFS

    C. Xor-tree Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem/C ...

  2. 2015 UESTC 搜索专题B题 邱老师降临小行星 记忆化搜索

    邱老师降临小行星 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Des ...

  3. 厄拉多塞筛法和普通方法求素数表(python实现)

    厄拉多赛筛法(sieve of Eratosthenes): 想要得到一个不大于N的数所有素数,可以先找到不超过根号N的所有素数,设2 = p1 < p2 < ......<pk ≤ ...

  4. hssworkbook 用法案例

    public ActionResult excelPrint() { HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件 HSSFShe ...

  5. Mastering stack and heap for system reliability

    http://www.iar.com/Global/Resources/Developers_Toolbox/Building_and_debugging/Mastering_stack_and_he ...

  6. IDA设置函数类型

    http://www.2cto.com/shouce/ida/1361.htm Action name: SetType 该命令允许你指定当前条目类型. 如果光标处在函数内部,那么函数类型将会被编辑, ...

  7. 详解Java中的clone方法 -- 原型模式 及源码

    http://www.cnblogs.com/cq-home/p/6431426.html http://blog.csdn.net/zhangjg_blog/article/details/1836 ...

  8. The differentiation program with abstract data

    #!r6rs ( import ( rnrs base ( 6 ) )          ( rnrs io simple ( 6 ) ) ) ( define ( deriv exp var )   ...

  9. MYSQL Out of resources when opening file './xxx.MYD' (Errcode: 24)

    出现Out of resources when opening file './xxx.MYD' (Errcode: 24)错误是因为打开的文件数超过了my.cnf的--open-files-limi ...

  10. linux shell 正则表达式(BREs,EREs,PREs)差异比较(转,当作资料查)

    转载: 在计算机科学中,是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串.在很多文本编辑器或其他工具里,正则表达式通常被用来检索和/或 替换那些符合某个模式的文本内容.许多程序设计语 ...