Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,15 @@ describe('Service/Utilies', () => {
expect(output2).toBe('12,345,678');
});

it('should return a formatted string wrapped in parentheses when the input number is negative and the displayNegativeNumberWithParentheses argument is enabled', () => {
const input = -123;
const displayNegativeNumberWithParentheses = true;
const output = formatNumber(input, 2, 2, displayNegativeNumberWithParentheses);
expect(output).toBe('(123.00)');
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', () => {
const input = 12345678;
const decimalSeparator = ',';
const thousandSeparator = '.';
const output1 = formatNumber(input, null, null, false, '', '', decimalSeparator, thousandSeparator);
const output2 = formatNumber(input, undefined, undefined, false, '', '', decimalSeparator, thousandSeparator);

expect(output1).toBe('12.345.678');
expect(output2).toBe('12.345.678');
});

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

it('should return a formatted currency string and thousand separator using dot (.) and decimal using comma (,) when those are provided', () => {
const input = -12345678.32;
const displayNegativeNumberWithParentheses = false;
const currencyPrefix = '$';
const decimalSeparator = ',';
const thousandSeparator = '.';
const output = formatNumber(input, 2, 2, displayNegativeNumberWithParentheses, currencyPrefix, '', decimalSeparator, thousandSeparator);
expect(output).toBe('-$12.345.678,32');
});

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', () => {
const input = -1234;
const displayNegativeNumberWithParentheses = true;
Expand Down
21 changes: 17 additions & 4 deletions src/app/modules/angular-slickgrid/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,29 @@ export function decimalFormatted(input: number | string, minDecimal?: number, ma
amount += '0';
}

const decimalSplit = amount.split('.');
let integerNumber;
let decimalNumber;

// do we want to display our number with a custom separator in each thousand position
if (thousandSeparator) {
amount = thousandSeparatorFormatted(amount, thousandSeparator);
integerNumber = decimalSplit.length >= 1 ? thousandSeparatorFormatted(decimalSplit[0], thousandSeparator) : undefined;
} else {
integerNumber = decimalSplit.length >= 1 ? decimalSplit[0] : amount;
}

// when using a separator that is not a dot, replace it with the new separator
if (decimalSeparator !== '.') {
amount = amount.replace('.', decimalSeparator);
if (decimalSplit.length > 1) {
decimalNumber = decimalSplit[1];
}

let output = '';
if (integerNumber !== undefined && decimalNumber !== undefined) {
output = `${integerNumber}${decimalSeparator}${decimalNumber}`;
} else if (integerNumber !== undefined && integerNumber !== null) {
output = integerNumber;
}
return amount;
return output;
}

/**
Expand Down