Skip to content

Commit 2db9702

Browse files
committed
chore(app): number separator formatting
1 parent 3fba659 commit 2db9702

3 files changed

Lines changed: 5 additions & 2 deletions

File tree

cli/src/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ export function estimateRequests(balanceMills: number) {
158158

159159
export function formatCost(tokens: number, perMillionDollars: number) {
160160
const cost = estimateCost(tokens, perMillionDollars)
161-
if (cost >= 0.01) return cost.toFixed(2)
161+
if (cost >= 0.01)
162+
return cost.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })
162163
if (cost === 0) return '0.0'
163164
const decimals = Math.min(4, Math.max(2, -Math.floor(Math.log10(cost)) + 1))
164165
const factor = 10 ** decimals

src/lib/format.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ test('formatCost returns 2 decimal places for costs >= $0.01', () => {
55
expect(formatCost(1_000_000, 3)).toBe('3.00')
66
expect(formatCost(500_000, 3)).toBe('1.50')
77
expect(formatCost(10_000_000, 0.5)).toBe('5.00')
8+
expect(formatCost(462_320_000, 3)).toBe('1,386.96')
89
})
910

1011
test('formatCost returns trimmed 4 decimal places for costs < $0.01', () => {

src/lib/format.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export function formatDollars(dollars: number) {
2626

2727
export function formatCost(tokens: number, perMillionDollars: number) {
2828
const cost = estimateCost(tokens, perMillionDollars)
29-
if (cost >= 0.01) return cost.toFixed(2)
29+
if (cost >= 0.01)
30+
return cost.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })
3031
if (cost === 0) return '0.0'
3132
const decimals = Math.min(4, Math.max(2, -Math.floor(Math.log10(cost)) + 1))
3233
const factor = 10 ** decimals

0 commit comments

Comments
 (0)