@icgio/icg-utils 1.9.65 → 1.9.66

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/lib/utils.js +17 -0
  2. package/package.json +1 -1
package/lib/utils.js CHANGED
@@ -641,6 +641,22 @@ function number_with_commas(x) {
641
641
  return parts.join('.')
642
642
  }
643
643
 
644
+ // Format a number for display with magnitude-aware precision: show roughly `sig_figs`
645
+ // significant figures, but always between `min_decimals` and `max_decimals` decimal places,
646
+ // with trailing zeros trimmed back down to `min_decimals`. Optionally group the integer part
647
+ // with commas. Keeps small amounts (e.g. 0.0000952) visible while sparing large amounts
648
+ // (e.g. 985,864,704.82) a tail of meaningless digits.
649
+ function format_amount(value, { sig_figs = 6, min_decimals = 2, max_decimals = 8, commas = true } = {}) {
650
+ let num = typeof value === 'string' ? parseFloat(value) : value
651
+ if (!Number.isFinite(num)) return String(value)
652
+ let decimals = _.clamp(sig_figs - 1 - Math.floor(Math.log10(Math.abs(num))), min_decimals, max_decimals)
653
+ let str = num.toFixed(decimals)
654
+ if (str.includes('.')) {
655
+ str = str.replace(new RegExp(`(\\.\\d{${min_decimals}}\\d*?)0+$`), '$1').replace(/\.$/, '')
656
+ }
657
+ return commas ? number_with_commas(str) : str
658
+ }
659
+
644
660
  // =============================================================================
645
661
  // Exchange Key Utilities
646
662
  //
@@ -897,6 +913,7 @@ module.exports = {
897
913
  reverse_pair,
898
914
  cur_dict_to_pair_list,
899
915
  number_with_commas,
916
+ format_amount,
900
917
  normalize_exchange_key,
901
918
  is_exchange_account_key,
902
919
  parse_exchange_account_key,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.9.65",
3
+ "version": "1.9.66",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {