Skip to content

Commit

Permalink
Merge pull request #185 from oat-sa/fix/TR-6116/parseFloat-thousandsS…
Browse files Browse the repository at this point in the history
…eparator-fix

Fix/tr 6116/parse float thousands separator fix
  • Loading branch information
wazelin authored Jun 13, 2024
2 parents c2ae7b3 + ea25f27 commit 32faab2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/util/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ export default {

// discard all thousand separators:
if (thousandsSeparator.length) {
numStr = numStr.replace(new RegExp(`\\${thousandsSeparator}`, 'g'), '');
// This regex finds thousands separators between groups of three digits
const thousandsRegex = new RegExp(`(?<=\\d)(?<!\\.\\d{0,2})\\${thousandsSeparator}(?=\\d{3}(\\D|$))`, 'g');
numStr = numStr.replace(thousandsRegex, '');
}

// standardise the decimal separator as '.':
Expand Down
25 changes: 25 additions & 0 deletions test/util/locale/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,29 @@ define([
})
.then(ready);
});

QUnit.test('parseFloat with thousands separator in decimal part', assert => {
locale.setConfig({
decimalSeparator: '.',
thousandsSeparator: ','
});

assert.equal(locale.parseFloat('3,14'), 3.0, 'float with thousands separator in decimal part should be parsed as integer 3');
assert.equal(locale.parseFloat('34,1'), 34.0, 'float with thousands separator in decimal part should be parsed as integer 34');
assert.equal(locale.parseFloat('3,123'), 3123.0, 'integer part with thousands separator should be parsed correctly as 3123');
assert.equal(locale.parseFloat('23,123'), 23123.0, 'integer part with thousands separator should be parsed correctly as 23123');

assert.equal(locale.parseFloat('1,000'), 1000.0, 'integer with one thousands separator');
assert.equal(locale.parseFloat('1,000,000'), 1000000.0, 'integer with multiple valid thousands separators');
assert.equal(locale.parseFloat('1,00,000'), 1.0, 'integer with invalid thousands separators');
assert.equal(locale.parseFloat('1,000.123'), 1000.123, 'float with one thousands separator');
assert.equal(locale.parseFloat('1,000,000.123'), 1000000.123, 'float with multiple valid thousands separators');
assert.equal(locale.parseFloat('1,00,000.123'), 1.0, 'float with invalid thousands separators');
assert.equal(locale.parseFloat('1,000.1,23'), 1000.1, 'float with invalid thousands separator in the decimal part');

assert.equal(locale.parseFloat('1000,'), 1000.0, 'trailing thousands separator should be ignored');
assert.equal(locale.parseFloat('1,,000'), 1.0, 'double thousands separator should be invalid');
assert.equal(locale.parseFloat('1,000,000.0001'), 1000000.0001, 'float with multiple valid thousands separators and small decimal part');
assert.equal(locale.parseFloat('1,000,000,000.0001'), 1000000000.0001, 'float with many valid thousands separators and small decimal part');
});
});

0 comments on commit 32faab2

Please sign in to comment.