Skip to content

Commit

Permalink
Fix Scientific notation allowing it by default (#5909)
Browse files Browse the repository at this point in the history
* Fix Scientific notation allowing it by default

* Update text-mask-addons to formio.4

* Add yarn.lock to fix/FIO-9173-notation

* Fix tests on NaN inputs like {} []

---------

Co-authored-by: lane-formio <lane@form.io>
  • Loading branch information
edwinanciani and lane-formio authored Dec 17, 2024
1 parent 7d4e90c commit 0d6767f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
17 changes: 12 additions & 5 deletions src/components/number/Number.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,21 @@ export default class NumberComponent extends Input {
if (typeof input === 'string') {
input = input.split(this.delimiter).join('').replace(this.decimalSeparator, '.');
}
let value = parseFloat(input);
let value;

if (!_.isNaN(value)) {
if (!_.isNaN(input)) {
// Format scientific notation
if (/e/i.test(String(value))) {
value = value.toExponential(this.decimalLimit);
if (/[0-9]+[eE]/.test(String(input))) {
// Convert to exponential notation will depend on the decimal limit set in the component
// Example: 1.23e-5 will be converted to 1.23e-5 if decimal limit is set to 2
// Example: 1.23e5 will be converted to 1.23e+5 if decimal limit is set to 2
// if decimal limit is 3, 1.23e5 will be converted to 1.230e+5
// if decimal limit is not set, 1.23e5 will be converted to 1.23000000000000000000e+5
value = parseFloat(input);
value = value.toExponential(this.decimalLimit);
} else {
value = String(value).replace('.', this.decimalSeparator);
value = parseFloat(input);
value = !_.isNaN(value) ? String(value).replace('.', this.decimalSeparator) : null;
}
}
else {
Expand Down
16 changes: 7 additions & 9 deletions test/unit/Number.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,16 @@ describe('Number Component', () => {
});

it('Should correctly handle scientific notation', () => {
return Harness.testCreate(NumberComponent, scientificNotation, { allowScientificNotation: true }).then((component) => {
return Harness.testCreate(NumberComponent, scientificNotation).then((component) => {
const testCases = [
[6.54635E+12, 6546350000000, '6546350000000'],
[1.23e-5, 0.0000123, '0.0000123'],
[3.14159e2, 314.159, '314.159'],
[2e-3, 0.002, '0.002'],
[7.5e5, 750000, '750000'],
[1.2345e10, 12345000000, '12345000000'],
['6.54e+12', 6.54e+12, '6.54e+12'],
['1.23e-5', 1.23e-5, '1.23e-5'],
['3.14e+2', 3.14e+2, '3.14e+2'],
['2e-3', 2e-3, '2e-3'],
['7.5e+5', 7.5e+5, '7.5e+5'],
['1.23e+10', 1.23e+10, '1.23e+10'],
];

testCases.forEach(([input, expectedValue, expectedDisplayValue]) => {

component.setValue(input);
assert.equal(component.dataValue, expectedValue, `setValue: ${input} should result in ${expectedValue}`);
assert.equal(component.getValueAsString(input), expectedDisplayValue, `getValueAsString: ${input} should result in ${expectedDisplayValue}`);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/fixtures/number/scientificNotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
'spellcheck': true,
'tableView': true,
'delimiter': true,
'requireDecimal': true,
'requireDecimal': false,
'inputFormat': 'plain',
'key': 'number',
'type': 'number',
Expand Down

0 comments on commit 0d6767f

Please sign in to comment.