xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub! js 小数转整数,避免精度损失 bug

const arr = [
  0.01, 0.01, 0.01,
  0.01, 0.01, 0.01,
  0.01, 0.01, 0.01,
  0.01
];
// (10) [0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01]

// 现扩大成为整数,再还原到小数
arr.map(i => i * 100).reduce((acc, i) => acc += i) / 100;
// 0.1


xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
js 小数转整数,避免精度损失 bug

test

    const temp = shuffle(result).map(i => i / multi);
    // log(`temp =`, temp);
    const total = temp.reduce((acc, i) => acc += i*multi, 0) / multi;
    // log(`total !== originMoney`, total !== originMoney, total, originMoney);

红包算法

refs

最简单的方法实现微信红包的随机算法

https://www.cnblogs.com/xgqfrms/p/13688375.html



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!