Number.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { createVNode as _createVNode } from "vue";
  2. const StatisticNumber = props => {
  3. const {
  4. value,
  5. formatter,
  6. precision,
  7. decimalSeparator,
  8. groupSeparator = '',
  9. prefixCls
  10. } = props;
  11. let valueNode;
  12. if (typeof formatter === 'function') {
  13. // Customize formatter
  14. valueNode = formatter({
  15. value
  16. });
  17. } else {
  18. // Internal formatter
  19. const val = String(value);
  20. const cells = val.match(/^(-?)(\d*)(\.(\d+))?$/);
  21. // Process if illegal number
  22. if (!cells) {
  23. valueNode = val;
  24. } else {
  25. const negative = cells[1];
  26. let int = cells[2] || '0';
  27. let decimal = cells[4] || '';
  28. int = int.replace(/\B(?=(\d{3})+(?!\d))/g, groupSeparator);
  29. if (typeof precision === 'number') {
  30. decimal = decimal.padEnd(precision, '0').slice(0, precision > 0 ? precision : 0);
  31. }
  32. if (decimal) {
  33. decimal = `${decimalSeparator}${decimal}`;
  34. }
  35. valueNode = [_createVNode("span", {
  36. "key": "int",
  37. "class": `${prefixCls}-content-value-int`
  38. }, [negative, int]), decimal && _createVNode("span", {
  39. "key": "decimal",
  40. "class": `${prefixCls}-content-value-decimal`
  41. }, [decimal])];
  42. }
  43. }
  44. return _createVNode("span", {
  45. "class": `${prefixCls}-content-value`
  46. }, [valueNode]);
  47. };
  48. StatisticNumber.displayName = 'StatisticNumber';
  49. export default StatisticNumber;