Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(web): fix unit promotion in formatNumberAbbreviated (#27918)

NeatGuyCodinga79853433799
diff --git a/web/utils/format.spec.ts b/web/utils/format.spec.ts
index 20e54fe1a4e..13c58bd7e59 100644
--- a/web/utils/format.spec.ts
+++ b/web/utils/format.spec.ts
@@ -121,7 +121,7 @@ describe('formatNumberAbbreviated', () => {
expect(formatNumberAbbreviated(1000000)).toBe('1M')
expect(formatNumberAbbreviated(1500000)).toBe('1.5M')
expect(formatNumberAbbreviated(2300000)).toBe('2.3M')
- expect(formatNumberAbbreviated(999999999)).toBe('1000M')
+ expect(formatNumberAbbreviated(999999999)).toBe('1B')
})
it('should format billions with B suffix', () => {
@@ -145,7 +145,7 @@ describe('formatNumberAbbreviated', () => {
it('should handle edge cases', () => {
expect(formatNumberAbbreviated(950)).toBe('950')
expect(formatNumberAbbreviated(1001)).toBe('1k')
- expect(formatNumberAbbreviated(999999)).toBe('1000k')
+ expect(formatNumberAbbreviated(999999)).toBe('1M')
})
})
diff --git a/web/utils/format.ts b/web/utils/format.ts
index 7fcef3669ff..fe5b1deb7da 100644
--- a/web/utils/format.ts
+++ b/web/utils/format.ts
@@ -130,10 +130,20 @@ export const formatNumberAbbreviated = (num: number) => {
for (let i = 0; i < units.length; i++) {
if (num >= units[i].value) {
- const formatted = (num / units[i].value).toFixed(1)
+ const value = num / units[i].value
+ let rounded = Math.round(value * 10) / 10
+ let unitIndex = i
+
+ // If rounded value >= 1000, promote to next unit
+ if (rounded >= 1000 && i > 0) {
+ rounded = rounded / 1000
+ unitIndex = i - 1
+ }
+
+ const formatted = rounded.toFixed(1)
return formatted.endsWith('.0')
- ? `${Number.parseInt(formatted)}${units[i].symbol}`
- : `${formatted}${units[i].symbol}`
+ ? `${Number.parseInt(formatted)}${units[unitIndex].symbol}`
+ : `${formatted}${units[unitIndex].symbol}`
}
}
}