• toFixed()与银行家舍入


    toFixed()与银行家舍入

    一直在用toFixed()方法做浮点数的舍入取值,如果只是客户端展示数据是没有多大问题的,但是如果涉及到和后端互交,数据的精度可能会导致接口对接失败,当然了,涉及安全性的数值,比如金额之类的不应该放在前端计算,应该以后端为准,少数情况下如果需要的时候,则需要修复其精度

    1.出现问题的场景

    1. 首先,我们发现在ie浏览器与其他的主流浏览器中,由于二进制下浮点数的存储问题,toFixed()的行为是不一样的,也说明了各浏览器厂家的做法不一致。

    在ie11中:

    10.015.toFixed(2)
    2// 打印结果:"0.02"

    在chrome中:

    10.015.toFixed(2)
    2// 打印结果:"0.01"

    对于此类问题,部分场景是不可接受的,测试可能会打出一个兼容性的bug

    1. 其次,我们虽然知道,toFixed()是一种银行家舍入,但是他确是入五取单,而不是银行家舍入所说的入五取双,同样是银行家舍入,此函数计算精度与后台java等语言的银行家舍入精度不同,可能照成接口验证失败。

    在前端浏览器中:

    10.025.toFixed(2)
    2// 打印结果:"0.03"
    30.035.toFixed(2)
    4// 打印结果:"0.03"

    在后端服务器中:

    10.025.setScale(2,RoundingMode.HALF_EVEN)
    2// 打印结果:"0.02"
    30.035.setScale(2,RoundingMode.HALF_EVEN)
    4// 打印结果:"0.04"

    事实上,正确的做法应该是:有关金额的运算应该完全交由后端计算,然后交给前端展示

    2.修复方案

    方案很简单,我们只要重写Number.prototype.toFixed方法就可以了。

    1. 如果我们需要普通的四舍五入:
     1/* eslint-disable no-extend-native */ // 规避eslint不可修改原型报错
    2Number.prototype.originalToFixed = Number.prototype.toFixed // 保留原方法
    3Number.prototype.toFixed = function(length = 0// 默认保留0位小数
    4  let result
    5  // 处理负数
    6  let that = this
    7  let isNegative = false
    8  if (this < 0) {
    9    isNegative = true
    10    that = -this
    11  }
    12  let thisNum = that.toString()
    13  if (thisNum.indexOf('.') === -1) thisNum = thisNum + '.0'
    14  for (let i = 0; i < length; i++) {
    15    thisNum = thisNum + '0'
    16  }
    17  const dotIndex = thisNum.indexOf('.')
    18  thisNum = thisNum.slice(0, dotIndex) + thisNum.slice((dotIndex + 1), (dotIndex + 1 + length)) + '.' + thisNum.slice(dotIndex + 1 + length)
    19  thisNum = Number(thisNum).toString()
    20  const thisNumList = thisNum.split('.'// 分离整数与小数
    21  if (thisNumList.length === 1) result = thisNum.toString() // 如果只有整数,则不需要处理
    22  else {
    23    if (Number(thisNumList[1][0]) >= 5) result = (Number(thisNumList[0]) + 1).toString() // 五入
    24    else result = thisNumList[0// 四舍
    25  }
    26  if (length === 0return `${isNegative ? '-' : ''}${result}`
    27  else {
    28    while (result.length < length + 1) { // 如果位数不够,则用0补齐
    29      result = '0' + result
    30    }
    31    return result.slice(0, (result.length - length)) + '.' + result.slice(result.length - length)
    32  }
    33}
    1. 如果我们需要使用正常的银行家舍入:
     1/* eslint-disable no-extend-native */ // 规避eslint不可修改原型报错
    2Number.prototype.originalToFixed = Number.prototype.toFixed // 保留原方法
    3Number.prototype.toFixed = function(length = 0// 默认保留0位小数
    4  let result
    5  // 处理负数
    6  let that = this
    7  let isNegative = false
    8  if (this < 0) {
    9    isNegative = true
    10    that = -this
    11  }
    12  let thisNum = that.toString()
    13  if (thisNum.indexOf('.') === -1) thisNum = thisNum + '.0'
    14  for (let i = 0; i < length; i++) {
    15    thisNum = thisNum + '0'
    16  }
    17  const dotIndex = thisNum.indexOf('.')
    18  thisNum = thisNum.slice(0, dotIndex) + thisNum.slice((dotIndex + 1), (dotIndex + 1 + length)) + '.' + thisNum.slice(dotIndex + 1 + length)
    19  thisNum = Number(thisNum).toString()
    20  const thisNumList = thisNum.split('.'// 分离整数与小数
    21  if (thisNumList.length === 1) result = thisNum.toString() // 如果只有整数,则不需要处理
    22  else {
    23    if (Number(thisNumList[1][0]) > 5) result = (Number(thisNumList[0]) + 1).toString() // 六入
    24    else if (Number(thisNumList[1][0]) < 5) result = thisNumList[0// 四舍
    25    else { // 判断5的情况
    26      if (thisNumList[1].length > 1) result = (Number(thisNumList[0]) + 1).toString() // 如果5后还有位数则入
    27      else {
    28        if (Number(thisNumList[0][thisNumList[0].length - 1]) % 2 === 0) result = thisNumList[0// 五前为偶应舍去
    29        else result = (Number(thisNumList[0]) + 1).toString() // 五前为奇要进一
    30      }
    31    }
    32  }
    33  if (length === 0return `${isNegative ? '-' : ''}${result}`
    34  else {
    35    while (result.length < length + 1) { // 如果位数不够,则用0补齐
    36      result = '0' + result
    37    }
    38    return `${isNegative ? '-' : ''}${result.slice(0, (result.length - length)) + '.' + result.slice(result.length - length)}`
    39  }
    40}

    这样就统一了舍入的精度,可以根据后台需要选择。


  • 相关阅读:
    中学数学
    XBT Tracker 服务器配置
    【转】>Unity3d动态数据管理(Export AssetBundles)
    [转]MySQL 5.1.56 for Windows XP/Vista/7
    [转]全面理解Unity加载和内存管理
    [转]Unity3D Editor 编辑器简易教程
    Dow falls 97 points, worst showing this year
    [转]Boost库编译后命名方式
    free falling
    01、Direct3D 11 Basics
  • 原文地址:https://www.cnblogs.com/qinyuandong/p/14285926.html
Copyright © 2020-2023  润新知