math.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. var math = {};
  2. math.Division = function (num1, num2) {
  3. var t1 = 0, t2 = 0, r1, r2;
  4. try {
  5. t1 = num1.toString().split(".")[1].length;
  6. } catch (e) { }
  7. try {
  8. t2 = num2.toString().split(".")[1].length;
  9. } catch (e) { }
  10. r1 = Number(num1.toString().replace(".", ""));
  11. r2 = Number(num2.toString().replace(".", ""));
  12. return (r1 / r2) * Math.pow(10, t2 - t1);
  13. }
  14. // 乘法
  15. math.Multiplication = function (num1, num2) {
  16. var m = 0, s1 = num1.toString(), s2 = num2.toString();
  17. try {
  18. m += s1.split(".")[1].length;
  19. } catch (e) { }
  20. try {
  21. m += s2.split(".")[1].length;
  22. } catch (e) { }
  23. var v = Number(s1.replace(".", "")) * Number(s2.replace(".", ""));
  24. return (v / Math.pow(10, m)).toFixed(2);
  25. }
  26. // 加法
  27. math.Addition = function (num1, num2) {
  28. var r1 = 0, r2 = 0, m;
  29. try {
  30. r1 = num1.toString().split(".")[1].length;
  31. } catch (e) { }
  32. try {
  33. r2 = num2.toString().split(".")[1].length;
  34. } catch (e) { }
  35. m = Math.pow(10, Math.max(r1, r2));
  36. return ((num1 * m + num2 * m) / m).toFixed(2);
  37. }
  38. // 减法
  39. math.Subtraction = function (num1, num2) {
  40. var r1 = 0, r2 = 0, m, n;
  41. try {
  42. r1 = num1.toString().split(".")[1].length;
  43. } catch (e) { }
  44. try {
  45. r2 = num2.toString().split(".")[1].length;
  46. } catch (e) { }
  47. m = Math.pow(10, Math.max(r1, r2));
  48. n = (r1 >= r2) ? r1 : r2;
  49. return ((num1 * m - num2 * m) / m).toFixed(n);
  50. }
  51. export default math