Skip to content

Commit

Permalink
🐛 fix: fix pricing with 0 digit (#4964)
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx authored Dec 10, 2024
1 parent 3c34ced commit c1061b4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@
"@langchain/community": "^0.3.0",
"@lobehub/chat-plugin-sdk": "^1.32.4",
"@lobehub/chat-plugins-gateway": "^1.9.0",
"@lobehub/icons": "^1.38.1",
"@lobehub/icons": "^1.56.0",
"@lobehub/tts": "^1.25.1",
"@lobehub/ui": "^1.152.0",
"@lobehub/ui": "^1.153.13",
"@neondatabase/serverless": "^0.10.1",
"@next/third-parties": "^14.2.15",
"@react-spring/web": "^9.7.5",
Expand Down Expand Up @@ -194,7 +194,6 @@
"pwa-install-handler": "^2.6.1",
"query-string": "^9.1.1",
"random-words": "^2.0.1",
"re-resizable": "6.10.1",
"react": "^18.3.1",
"react-confetti": "^6.1.0",
"react-dom": "^18.3.1",
Expand Down
7 changes: 7 additions & 0 deletions src/utils/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ describe('format', () => {
expect(formatPrice(0.99)).toBe('0.99');
expect(formatPrice(1000000.01)).toBe('1,000,000.01');
});

it('should format prices with digits correctly', () => {
expect(formatPrice(1000, 1)).toBe('1,000.0');
expect(formatPrice(1234.56)).toBe('1,234.56');
expect(formatPrice(0.99)).toBe('0.99');
expect(formatPrice(1000000.01, 0)).toBe('1,000,000');
});
});

describe('formatPriceByCurrency', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ export const formatTokenNumber = (num: number): string => {
export const formatPrice = (price: number, fractionDigits: number = 2) => {
if (!price && price !== 0) return '--';

if (fractionDigits === 0) return numeral(price).format('0,0');

const [a, b] = price.toFixed(fractionDigits).split('.');
return `${numeral(a).format('0,0')}.${b}`;
};
Expand Down

0 comments on commit c1061b4

Please sign in to comment.