Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit 4aa9a91

Browse files
authored
Merge pull request #415 from ghiscoding/bugfix/decimal_thousand_separators
fix(formatters): decimalSeparator & thousandSeparator work tgt, fix #411
2 parents 72870f4 + af146fb commit 4aa9a91

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

src/app/modules/angular-slickgrid/services/__tests__/utilities.spec.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,15 @@ describe('Service/Utilies', () => {
351351
expect(output2).toBe('12,345,678');
352352
});
353353

354-
it('should return a formatted string wrapped in parentheses when the input number is negative and the displayNegativeNumberWithParentheses argument is enabled', () => {
355-
const input = -123;
356-
const displayNegativeNumberWithParentheses = true;
357-
const output = formatNumber(input, 2, 2, displayNegativeNumberWithParentheses);
358-
expect(output).toBe('(123.00)');
354+
it('should return a string without decimals but using dot (.) as thousand separator when these arguments are null or undefined and the input provided is an integer', () => {
355+
const input = 12345678;
356+
const decimalSeparator = ',';
357+
const thousandSeparator = '.';
358+
const output1 = formatNumber(input, null, null, false, '', '', decimalSeparator, thousandSeparator);
359+
const output2 = formatNumber(input, undefined, undefined, false, '', '', decimalSeparator, thousandSeparator);
360+
361+
expect(output1).toBe('12.345.678');
362+
expect(output2).toBe('12.345.678');
359363
});
360364

361365
it('should return a formatted string wrapped in parentheses when the input number is negative and the displayNegativeNumberWithParentheses argument is enabled', () => {
@@ -392,6 +396,16 @@ describe('Service/Utilies', () => {
392396
expect(output).toBe('-$12,345,678.00');
393397
});
394398

399+
it('should return a formatted currency string and thousand separator using dot (.) and decimal using comma (,) when those are provided', () => {
400+
const input = -12345678.32;
401+
const displayNegativeNumberWithParentheses = false;
402+
const currencyPrefix = '$';
403+
const decimalSeparator = ',';
404+
const thousandSeparator = '.';
405+
const output = formatNumber(input, 2, 2, displayNegativeNumberWithParentheses, currencyPrefix, '', decimalSeparator, thousandSeparator);
406+
expect(output).toBe('-$12.345.678,32');
407+
});
408+
395409
it('should return a formatted currency string with symbol prefix/suffix wrapped in parentheses when the input number is negative, when all necessary arguments are filled', () => {
396410
const input = -1234;
397411
const displayNegativeNumberWithParentheses = true;

src/app/modules/angular-slickgrid/services/utilities.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,29 @@ export function decimalFormatted(input: number | string, minDecimal?: number, ma
189189
amount += '0';
190190
}
191191

192+
const decimalSplit = amount.split('.');
193+
let integerNumber;
194+
let decimalNumber;
195+
192196
// do we want to display our number with a custom separator in each thousand position
193197
if (thousandSeparator) {
194-
amount = thousandSeparatorFormatted(amount, thousandSeparator);
198+
integerNumber = decimalSplit.length >= 1 ? thousandSeparatorFormatted(decimalSplit[0], thousandSeparator) : undefined;
199+
} else {
200+
integerNumber = decimalSplit.length >= 1 ? decimalSplit[0] : amount;
195201
}
196202

197203
// when using a separator that is not a dot, replace it with the new separator
198-
if (decimalSeparator !== '.') {
199-
amount = amount.replace('.', decimalSeparator);
204+
if (decimalSplit.length > 1) {
205+
decimalNumber = decimalSplit[1];
206+
}
207+
208+
let output = '';
209+
if (integerNumber !== undefined && decimalNumber !== undefined) {
210+
output = `${integerNumber}${decimalSeparator}${decimalNumber}`;
211+
} else if (integerNumber !== undefined && integerNumber !== null) {
212+
output = integerNumber;
200213
}
201-
return amount;
214+
return output;
202215
}
203216

204217
/**

0 commit comments

Comments
 (0)