Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(mcp-host): print the decimal places a chart caller sets

marcoteb723a2be23419
diff --git a/mcp-host/src/workflow/__tests__/chartPlugins.test.ts b/mcp-host/src/workflow/__tests__/chartPlugins.test.ts
index 4d953985d..c3f36ad4c 100644
--- a/mcp-host/src/workflow/__tests__/chartPlugins.test.ts
+++ b/mcp-host/src/workflow/__tests__/chartPlugins.test.ts
@@ -58,9 +58,13 @@ describe('formatValue', () => {
expect(fmt(12, { format: 'percent' })).toBe('12%')
})
- it('honours an explicit decimal count', () => {
+ it('prints exactly the decimal count the caller set', () => {
expect(fmt(3.14159, { decimals: 2 })).toBe('3.14')
- expect(fmt(3, { decimals: 2 })).toBe('3')
+ expect(fmt(3, { decimals: 2 })).toBe('3.00')
+ expect(fmt(4.9, { format: 'currency', decimals: 2 })).toBe('$4.90')
+ expect(fmt(45, { format: 'percent', decimals: 1 })).toBe('45.0%')
+ expect(fmt(1_250_000, { format: 'compact', decimals: 2 })).toBe('1.25M')
+ expect(fmt(4.5, { format: 'currency', decimals: 0 })).toBe('$5')
})
it('keeps small fractions readable without an explicit count', () => {
diff --git a/mcp-host/src/workflow/chartPlugins.ts b/mcp-host/src/workflow/chartPlugins.ts
index 7ea9aa1c6..e8e81860a 100644
--- a/mcp-host/src/workflow/chartPlugins.ts
+++ b/mcp-host/src/workflow/chartPlugins.ts
@@ -39,11 +39,15 @@ const COMPACT_STEPS: Array<[number, string]> = [
export function formatValue(value: number, o: ValueLabelOptions): string {
if (!Number.isFinite(value)) return ''
const decimals = o.decimals
+ // A count the caller set prints exactly; a count guessed per value drops its
+ // trailing zeros, so 45% does not read as 45.0%.
const fixed = (n: number, d: number): string =>
- n
- .toFixed(d)
- .replace(/\.0+$/, '')
- .replace(/(\.\d*?)0+$/, '$1')
+ decimals !== undefined
+ ? n.toFixed(d)
+ : n
+ .toFixed(d)
+ .replace(/\.0+$/, '')
+ .replace(/(\.\d*?)0+$/, '$1')
switch (o.format) {
case 'percent':
@@ -59,7 +63,8 @@ export function formatValue(value: number, o: ValueLabelOptions): string {
if (abs >= step) return `${sign}${sym}${fixed(abs / step, decimals ?? 1)}${suffix}`
}
}
- // Cents are kept, both places: $4.99 must not read as $5, nor $4.90 as $4.9.
+ // Unless a count is set, cents are kept, both places: $4.99 must not read
+ // as $5, nor $4.90 as $4.9.
const places = decimals ?? (Number.isInteger(abs) ? 0 : Math.max(2, guessDecimals(abs)))
const shown =
decimals === undefined && places > 0