放肆青春的博客
首页
前端
算法
网络
面试
技术
后端
运维
杂项
数据库
工具
网址
电脑
个人
文章
  • 分类
  • 标签
  • 归档
github (opens new window)
gitee (opens new window)

放肆青春

一个前端菜鸟的技术成长之路
首页
前端
算法
网络
面试
技术
后端
运维
杂项
数据库
工具
网址
电脑
个人
文章
  • 分类
  • 标签
  • 归档
github (opens new window)
gitee (opens new window)
  • 前端

    • 前端 概览
    • 前端汇总

    • front 博文

    • front 项目总结

    • front 高级

    • front tools

  • vue

    • vue 概览
    • vue 汇总

    • vue 博文

    • vue 项目总结

    • vue 高级

  • html

    • html 概览
    • html 汇总

    • html 博文

  • css

    • css 概览
    • css 汇总

    • css 博文

    • sass

    • less

  • js

    • javascript 概览
    • JS 汇总

    • ES6

    • JS 博文

    • JS 工具

      • js工具
      • 图片工具
      • compute
        • js计算工具
          • js浮点数计算
      • 日期工具
      • moment日期工具
      • 存储工具
  • node

    • node 概览
    • node 汇总

    • node 框架

    • node 博文

  • react

    • react 概览
    • react 汇总

    • react 博文

    • react 高级

  • 微信小程序

    • 微信小程序 概览
    • 微信小程序总结
    • 微信小程序文章
    • 微信小程序 博文

    • 微信小程序 高级

  • 微信公众号

    • 微信公众号 概览
    • 微信公众号总结
    • 微信公众号文章
  • 多端开发

    • 多端开发
    • dsbridge 概览
    • jsbridge 概览
    • webview
    • uniapp

      • uniapp 概览
    • taro

      • taro 概览
    • flutter

      • flutter 概览
      • flutter 环境搭建
    • electron

      • electron 概览
  • front
放肆青春
2020-07-09

compute

# js计算工具

# js浮点数计算

// 浮点数加减乘除 https://blog.csdn.net/mafan121/article/details/81354735
/**
 * 浮点数加法
 * @param {} a
 * @param {*} b
 */
export const floatAdd = function(a, b) {
  let m = 0,
    n = 0, //记录a,b的小数位数
    d = a + "", //字符串化
    e = b + "";
  try {
    m = d.split(".")[1] ? d.split(".")[1].length : "";
  } catch (error) {
    console.log(error);
  }
  try {
    n = e.split(".")[1] ? e.split(".")[1].length : "";
  } catch (error) {
    console.log(error);
  }
  let maxInt = Math.pow(10, Math.max(m, n)); //将数字转换为整数的最大倍数
  return (floatMul(a, maxInt) + floatMul(b, maxInt)) / maxInt;
};

/**
 * 浮点数乘法
 * @param {} a
 * @param {*} b
 */
export const floatMul = function(a, b) {
  let m = 0,
    n = 0, //记录a,b的小数位数
    d = a + "", //字符串化
    e = b + "";
  try {
    m = d.split(".")[1] ? d.split(".")[1].length : "";
  } catch (error) {
    console.log(error);
  }
  try {
    n = e.split(".")[1] ? e.split(".")[1].length : "";
  } catch (error) {
    console.log(error);
  }
  let maxInt = Math.pow(10, m + n); //将数字转换为整数的最大倍数
  return (Number(d.replace(".", "")) * Number(e.replace(".", ""))) / maxInt;
};

/**
 * 浮点数加法
 * @param {} a
 * @param {*} b
 */
export const floatDivision = function(a, b) {
  let m = 0,
    n = 0, //记录a,b的小数位数
    d = a + "", //字符串化
    e = b + "";
  try {
    m = d.split(".")[1] ? d.split(".")[1].length : "";
  } catch (error) {
    console.log(error);
  }
  try {
    n = e.split(".")[1] ? e.split(".")[1].length : "";
  } catch (error) {
    console.log(error);
  }
  let maxInt = Math.pow(10, Math.max(n, m)); //将数字转换为整数的最大倍数
  let aInt = floatMul(a, maxInt);
  let bInt = floatMul(b, maxInt);
  return aInt / bInt;
};

//小数减法
export const floatSub = (a, b) => {
  let m = 0,
    n = 0, //记录a,b的小数位数
    d = a + "", //字符串化
    e = b + "";
  try {
    m = d.split(".")[1] ? d.split(".")[1].length : "";
  } catch (error) {
    console.log(error);
  }
  try {
    n = e.split(".")[1] ? e.split(".")[1].length : "";
  } catch (error) {
    console.log(error);
  }
  let maxInt = Math.pow(10, Math.max(m, n)); //将数字转换为整数的最大倍数
  return (floatMul(a, maxInt) - floatMul(b, maxInt)) / maxInt;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
更新时间: 3/11/2021, 10:44:08 AM
图片工具
日期工具

← 图片工具 日期工具→

最近更新
01
前端权限管理
02-24
02
vue2指令
02-24
03
vue2 hook
02-24
更多文章>
Theme by Vdoing | Copyright © 2019-2022 放肆青春
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式