Skip to content

Commit

Permalink
Issue #299 : Handle decimal values starting with ' . ' (#340)
Browse files Browse the repository at this point in the history
* Handle decimal values starting with .

* Handle all decimal separators.
  • Loading branch information
ng185115 authored Feb 19, 2024
1 parent c5b0271 commit 3b91498
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/components/utils/__tests__/formatValue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 5 additions & 1 deletion src/components/utils/formatValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 3b91498

Please sign in to comment.