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

Commit 67318d5

Browse files
committed
fix(formatters): decimalSeparator & thousandSeparator work tgt, fix #411
- when using both together, 1 was cancelling the other. To fix this, I rewrote the code use text split by the decimal and then recombining them as a new text with necessary thousand & decimal separators
1 parent 00b0751 commit 67318d5

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

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

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

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');
363+
});
364+
354365
it('should return a formatted string wrapped in parentheses when the input number is negative and the displayNegativeNumberWithParentheses argument is enabled', () => {
355366
const input = -123;
356367
const displayNegativeNumberWithParentheses = true;
@@ -392,6 +403,16 @@ describe('Service/Utilies', () => {
392403
expect(output).toBe('-$12,345,678.00');
393404
});
394405

406+
it('should return a formatted currency string and thousand separator using dot (.) and decimal using comma (,) when those are provided', () => {
407+
const input = -12345678.32;
408+
const displayNegativeNumberWithParentheses = false;
409+
const currencyPrefix = '$';
410+
const decimalSeparator = ',';
411+
const thousandSeparator = '.';
412+
const output = formatNumber(input, 2, 2, displayNegativeNumberWithParentheses, currencyPrefix, '', decimalSeparator, thousandSeparator);
413+
expect(output).toBe('-$12.345.678,32');
414+
});
415+
395416
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', () => {
396417
const input = -1234;
397418
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 {
212+
output = integerNumber;
200213
}
201-
return amount;
214+
return output;
202215
}
203216

204217
/**

0 commit comments

Comments
 (0)