合同小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
844 B

  1. import { isNumber } from '../isNumber'
  2. /**
  3. *
  4. * @param num1
  5. * @param num2
  6. * @returns
  7. */
  8. export function floatAdd(num1 : number, num2 : number) : number {
  9. // 检查 num1 和 num2 是否为数字类型
  10. if (!(isNumber(num1) || isNumber(num2))) {
  11. console.warn('Please pass in the number type');
  12. return NaN;
  13. }
  14. let r1 : number, r2 : number, m : number;
  15. try {
  16. // 获取 num1 小数点后的位数
  17. r1 = num1.toString().split('.')[1].length;
  18. } catch (error) {
  19. r1 = 0;
  20. }
  21. try {
  22. // 获取 num2 小数点后的位数
  23. r2 = num2.toString().split('.')[1].length;
  24. } catch (error) {
  25. r2 = 0;
  26. }
  27. // 计算需要扩大的倍数
  28. m = Math.pow(10, Math.max(r1, r2));
  29. // 返回相加结果
  30. return (num1 * m + num2 * m) / m;
  31. }