推荐关注
› Meteor
› JSLint - a JavaScript code quality tool
› jsFiddle
› D3.js
› WebStorm
推荐书目
› JavaScript 权威指南第 5 版
› Closure: The Definitive Guide
gdtv

javascript 计算 123+Math.floor(123*0.008*100)/100+10 为什么等于 133.98000000000002 ?

  •  
  •   gdtv · Mar 15, 2017 · 3880 views
    This topic created in 3522 days ago, the information mentioned may be changed or developed.


    -------------------------

    更新:感谢1楼的指点,改成了这样:(123*100+Math.floor(123*0.008*100)+10*100)/100=133.98,不知有没有更好的方法?

    PS: 为什么markdown模式显示不出星号?
    16 replies  •  2017-03-16 00:00:41 +08:00
    jingniao
    jingniao
    Mar 15, 2017 via Android❤️1
    1
    典型的浮点数问题
    Reficul
    Reficul
    Mar 15, 2017 via Android
    2
    0.1+0.2!=0.3 ,浮点数无解。只能想办法减少误差
    0915240
    0915240
    Mar 15, 2017❤️1
    3
    这个礼拜的第二回了:

    http://0.30000000000000004.com/
    IamJ
    IamJ
    Mar 15, 2017 via iPhone
    4
    @0915240 这域名 666
    nyanyh
    nyanyh
    Mar 15, 2017
    5
    r#3 @0915240 这域名哈哈哈哈哈哈哈哈哈哈哈哈
    mcfog
    mcfog
    Mar 15, 2017
    6
    不用那么长,我也来个短的 0.58*100
    Shura
    Shura
    Mar 15, 2017 via Android
    7
    建议楼主去学习一下计算机组成原理(
    SoloCompany
    SoloCompany
    Mar 15, 2017
    8
    一大谜团 - 为啥楼主不用 append 也能编辑帖子?
    Mutoo
    Mutoo
    Mar 15, 2017
    9
    keyword: IEEE 754 浮点数计算
    SoloCompany
    SoloCompany
    Mar 15, 2017
    10
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round#Decimal_rounding

    然后你可以试一下
    Math.round10(123+Math.floor(123*0.008*100)/100+10, -10)
    Quaintjade
    Quaintjade
    Mar 15, 2017
    12
    @SoloCompany
    Mozilla 这个还只是一半,负数部分得自己写,因为 js 的 Math.Round()总是向正无穷大取整:
    If the fractional portion is exactly 0.5, the argument is rounded to the next integer in the direction of +∞

    比如 Math.round10(-1.005,-2)的结果是 -1 而不是 -1.01
    wly19960911
    wly19960911
    Mar 15, 2017 via Android
    13
    @SoloCompany 我没记错的话,短时间内能编辑的,
    codelegant
    codelegant
    Mar 15, 2017 via Android
    14
    @0915240 这个网站,的确非常的有意思。
    a87150
    a87150
    Mar 15, 2017 via Android
    15
    世界的错
    zhuziyi
    zhuziyi
    Mar 16, 2017
    16
    ![]( )
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5209 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 71ms · UTC 06:26 · PVG 14:26 · LAX 22:26 · JFK 01:26
    ♥ Do have faith in what you're doing.