computed.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import Vue from 'vue';
  2. import Decimal from 'decimal.js';
  3. const toNumber = (num) => {
  4. return new Decimal(num).toNumber();
  5. };
  6. const plus = (n1 = 0, n2 = 0) => {
  7. const number1 = new Decimal(n1);
  8. const number2 = new Decimal(n2);
  9. let result = number1.add(number2);
  10. result = result.toFixed(2, Decimal.ROUND_DOWN);
  11. return toNumber(result);
  12. };
  13. const minus = (n1 = 0, n2 = 0) => {
  14. const number1 = new Decimal(n1);
  15. const number2 = new Decimal(n2);
  16. let result = number1.minus(number2);
  17. result = result.toFixed(2, Decimal.ROUND_DOWN);
  18. return toNumber(result);
  19. };
  20. const multiply = (n1 = 0, n2 = 0) => {
  21. const number1 = new Decimal(n1);
  22. const number2 = new Decimal(n2);
  23. let result = number1.mul(number2);
  24. result = result.toFixed(2, Decimal.ROUND_DOWN);
  25. return toNumber(result);
  26. };
  27. const divide = (n1 = 0, n2 = 0) => {
  28. const number1 = new Decimal(n1);
  29. const number2 = new Decimal(n2);
  30. let result = number1.div(number2);
  31. result = result.toFixed(2, Decimal.ROUND_DOWN);
  32. return toNumber(result);
  33. };
  34. Vue.prototype.$plus = plus;
  35. Vue.prototype.$minus = minus;
  36. Vue.prototype.$multiply = multiply;
  37. Vue.prototype.$divide = divide;