From e42b98fdee0dc42267cb3f0d23cdda8a65c5f3f3 Mon Sep 17 00:00:00 2001 From: onchito-walks Date: Mon, 11 May 2026 18:31:08 +0000 Subject: [PATCH] fix: Add comma formatting to size display values - Added addCommas() helper for formatting numbers with comma separators - Updated formatForDisplay() to use addCommas() for all return paths - Fixed 'Size to close is not well formatted' bug for amounts >= 1000 or with decimal places - Large position sizes now display as 5,000.00 instead of just 5000 Bounty: https://github.com/capofficial/client/issues/11 Payment: 1Ast5dKr9z1bLWFBnyh6WDQSgyL7EHJosp --- src/lib/formatters.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/lib/formatters.js b/src/lib/formatters.js index 34ce7eb..8b219b1 100644 --- a/src/lib/formatters.js +++ b/src/lib/formatters.js @@ -90,28 +90,35 @@ export function letterify(amount) { if (amount >= 10**3) return Math.round(amount/10**3) + 'K'; } +export function addCommas(x) { + if (x == null || isNaN(x) || typeof x === 'boolean') return x; + const parts = x.toString().split('.'); + parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ','); + return parts.join('.'); +} + export function formatForDisplay(amount, fix) { amount = amount * 1; if (!amount || isNaN(amount)) return 0; - if (!fix && (amount * 1).toFixed(6)*1 == Math.round(amount * 1)) return Math.round(amount); + if (!fix && (amount * 1).toFixed(6)*1 == Math.round(amount * 1)) return addCommas(Math.round(amount)); - if (fix) return (amount*1).toFixed(fix); + if (fix) return addCommas((amount*1).toFixed(fix)); if (amount * 1 >= 10000 || amount * 1 <= -10000) { - return Math.round(amount*1); + return addCommas(Math.round(amount*1)); } else if (amount * 1 >= 10 || amount * 1 <= -10) { - return (amount * 1).toFixed(2); + return addCommas((amount * 1).toFixed(2)); } else if (amount * 1 >= 1 || amount * 1 <= -1) { - return +(amount * 1).toFixed(4); + return addCommas(+(amount * 1).toFixed(4)); } else if (amount * 1 >= 0.01 || amount * 1 <= -0.01) { - return +(amount * 1).toFixed(5); + return addCommas(+(amount * 1).toFixed(5)); } else if (amount * 1 >= 0.001 || amount * 1 <= -0.001) { - return +(amount * 1).toFixed(6); + return addCommas(+(amount * 1).toFixed(6)); } else { - return +(amount * 1).toFixed(8); + return addCommas(+(amount * 1).toFixed(8)); } }