From 3b91498332f2f88cec1957987354bfb908e3600d Mon Sep 17 00:00:00 2001 From: Navya Gandham <73491078+ng185115@users.noreply.github.com> Date: Mon, 19 Feb 2024 17:27:52 +0530 Subject: [PATCH] Issue #299 : Handle decimal values starting with ' . ' (#340) * Handle decimal values starting with . * Handle all decimal separators. --- src/components/utils/__tests__/formatValue.spec.ts | 12 ++++++++++++ src/components/utils/formatValue.ts | 6 +++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/components/utils/__tests__/formatValue.spec.ts b/src/components/utils/__tests__/formatValue.spec.ts index 3c624d0..a3ef590 100644 --- a/src/components/utils/__tests__/formatValue.spec.ts +++ b/src/components/utils/__tests__/formatValue.spec.ts @@ -203,6 +203,18 @@ describe('formatValue', () => { ).toEqual('$30'); }); + it('should prefix decimal values correctly with zero', () => { + expect( + formatValue({ + decimalSeparator: '.', + groupSeparator: ',', + decimalScale: 2, + prefix: '$', + value: '.02', + }) + ).toEqual('$0.02'); + }); + describe('negative values', () => { it('should handle negative values', () => { expect( diff --git a/src/components/utils/formatValue.ts b/src/components/utils/formatValue.ts index 52b20ca..e8a8de6 100644 --- a/src/components/utils/formatValue.ts +++ b/src/components/utils/formatValue.ts @@ -79,11 +79,15 @@ export const formatValue = (options: FormatValueOptions): string => { _value ); - const value = + let value = decimalSeparator !== '.' ? replaceDecimalSeparator(_value, decimalSeparator, isNegative) : _value; + if (decimalSeparator && decimalSeparator !== '-' && value.startsWith(decimalSeparator)) { + value = '0' + value; + } + const defaultNumberFormatOptions = { minimumFractionDigits: decimalScale || 0, maximumFractionDigits: 20,