@instadapp/avocado-base 0.0.17 → 0.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/utils/formatter.ts +22 -13
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instadapp/avocado-base",
3
- "version": "0.0.17",
3
+ "version": "0.0.18",
4
4
  "type": "module",
5
5
  "main": "./nuxt.config.ts",
6
6
  "types": "global.d.ts",
@@ -61,25 +61,34 @@ function getFractionDigits(value: string | number) {
61
61
  }
62
62
  }
63
63
 
64
- export function formatDecimal(
65
- value: string | number,
66
- fractionDigits = getFractionDigits(value)
67
- ) {
64
+ export function formatDecimal(value: string | number, fractionDigits?: number) {
68
65
  if (!value) {
69
66
  value = "0";
70
67
  }
71
- let formatter;
72
68
  if (lt(value, "0.0001") && gt(value, "0")) {
73
69
  return "< 0.0001";
74
70
  } else {
75
- formatter = new Intl.NumberFormat("en-US", {
76
- style: "decimal",
77
- minimumFractionDigits: fractionDigits,
78
- maximumFractionDigits: fractionDigits,
79
- });
80
- }
71
+ const num = toBN(value);
72
+ let decimals;
81
73
 
82
- const valueAsNumber = toBN(value).toNumber();
74
+ if (num.lt(1)) {
75
+ decimals = 8;
76
+ } else if (num.lt(10)) {
77
+ decimals = 6;
78
+ } else if (num.lt(100)) {
79
+ decimals = 4;
80
+ } else if (num.lt(1000)) {
81
+ decimals = 3;
82
+ } else if (num.lt(10000)) {
83
+ decimals = 2;
84
+ } else if (num.lt(100000)) {
85
+ decimals = 1;
86
+ } else {
87
+ decimals = 0;
88
+ }
83
89
 
84
- return formatter.format(valueAsNumber);
90
+ const formattedNumber = num.toFixed(fractionDigits || decimals);
91
+
92
+ return toBN(formattedNumber).toFormat();
93
+ }
85
94
  }