@visns-studio/visns-components 5.7.7 → 5.7.9

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.
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Format a number with thousand separators (commas)
3
+ * @param {number|string} value - The value to format
4
+ * @param {number} [decimals=2] - Number of decimal places
5
+ * @returns {string} - Formatted number string
6
+ */
7
+ export const formatNumber = (value, decimals = 2) => {
8
+ if (!value && value !== 0) return '';
9
+
10
+ // Parse the value to make sure it's a number
11
+ const numValue = parseFloat(String(value).replace(/[^0-9.-]+/g, ''));
12
+
13
+ if (isNaN(numValue)) return value;
14
+
15
+ // Format with specified decimal places and thousand separators
16
+ return numValue.toLocaleString('en-US', {
17
+ minimumFractionDigits: decimals,
18
+ maximumFractionDigits: decimals,
19
+ });
20
+ };
21
+
22
+ /**
23
+ * Format a number as currency with dollar sign and thousand separators
24
+ * @param {number|string} value - The value to format
25
+ * @returns {string} - Formatted currency string
26
+ */
27
+ export const formatCurrency = (value) => {
28
+ if (!value && value !== 0) return '';
29
+
30
+ // Parse the value to make sure it's a number
31
+ const numValue = parseFloat(String(value).replace(/[^0-9.-]+/g, ''));
32
+
33
+ if (isNaN(numValue)) return value;
34
+
35
+ // Format with 2 decimal places, thousand separators, and dollar sign
36
+ return (
37
+ '$' +
38
+ numValue.toLocaleString('en-US', {
39
+ minimumFractionDigits: 2,
40
+ maximumFractionDigits: 2,
41
+ })
42
+ );
43
+ };