🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(utils): keep tiny prices visible in formatPrice (#14235)
YuTengjing94767fddcbab
diff --git a/packages/utils/src/format.test.ts b/packages/utils/src/format.test.tsindex 3c684e6eca7..3f27bf6f007 100644--- a/packages/utils/src/format.test.ts+++ b/packages/utils/src/format.test.ts@@ -185,6 +185,19 @@ describe('format', () => {expect(formatPrice(0.99)).toBe('0.99');expect(formatPrice(1000000.01, 0)).toBe('1,000,000');});++ it('should expand precision when a positive price would round to zero', () => {+ expect(formatPrice(0.003625)).toBe('0.004');+ expect(formatPrice(0.0001)).toBe('0.0001');+ expect(formatPrice(0)).toBe('0.00');+ });++ it('should not throw RangeError for sub-1e-100 prices', () => {+ // Number.prototype.toFixed accepts digits in [0, 100]; without a clamp+ // Math.ceil(-Math.log10(price)) can exceed that and throw RangeError.+ expect(() => formatPrice(1e-101)).not.toThrow();+ expect(() => formatPrice(Number.MIN_VALUE)).not.toThrow();+ });});describe('formatPriceByCurrency', () => {diff --git a/packages/utils/src/format.ts b/packages/utils/src/format.tsindex c89297b205b..d30ffae7463 100644--- a/packages/utils/src/format.ts+++ b/packages/utils/src/format.ts@@ -25,26 +25,10 @@ export const formatSize = (bytes: number, fractionDigits: number = 1): string =>export const formatSpeed = (byte: number, fractionDigits = 2) => {if (!byte && byte !== 0) return '--';- let word = '';-- // Byte- if (byte <= 1000) {- word = byte.toFixed(fractionDigits) + ' Byte/s';- }- // KB- else if (byte / 1024 <= 1000) {- word = (byte / 1024).toFixed(fractionDigits) + ' KB/s';- }- // MB- else if (byte / 1024 / 1024 <= 1000) {- word = (byte / 1024 / 1024).toFixed(fractionDigits) + ' MB/s';- }- // GB- else {- word = (byte / 1024 / 1024 / 1024).toFixed(fractionDigits) + ' GB/s';- }-- return word;+ if (byte <= 1000) return byte.toFixed(fractionDigits) + ' Byte/s';+ if (byte / 1024 <= 1000) return (byte / 1024).toFixed(fractionDigits) + ' KB/s';+ if (byte / 1024 / 1024 <= 1000) return (byte / 1024 / 1024).toFixed(fractionDigits) + ' MB/s';+ return (byte / 1024 / 1024 / 1024).toFixed(fractionDigits) + ' GB/s';};export const formatTime = (timeInSeconds: number): string => {@@ -118,7 +102,15 @@ export const formatPrice = (price: number, fractionDigits: number = 2) => {if (fractionDigits === 0) return numeral(price).format('0,0');- const [a, b] = price.toFixed(fractionDigits).split('.');+ // Expand precision when a positive price would round to zero at the requested+ // precision (e.g. $0.003625 → "0.00"), so users can tell it isn't actually free.+ // Cap at 100 because Number.prototype.toFixed throws RangeError beyond that.+ let digits = fractionDigits;+ if (price > 0 && Number(price.toFixed(fractionDigits)) === 0) {+ digits = Math.min(100, Math.ceil(-Math.log10(price)));+ }++ const [a, b] = price.toFixed(digits).split('.');return `${numeral(a).format('0,0')}.${b}`;};