Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Scientific notation allowing it by default #5909

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"@formio/bootstrap": "3.0.0-dev.98.17ba6ea",
"@formio/choices.js": "^10.2.1",
"@formio/core": "2.1.0-dev.191.8c609ab",
"@formio/text-mask-addons": "^3.8.0-formio.3",
"@formio/text-mask-addons": "^3.8.0-formio.4",
"@formio/vanilla-text-mask": "^5.1.1-formio.1",
"abortcontroller-polyfill": "^1.7.5",
"autocompleter": "^8.0.4",
Expand Down
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
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,10 @@
lodash "^4.17.21"
moment "^2.29.4"

"@formio/text-mask-addons@^3.8.0-formio.3":
version "3.8.0-formio.3"
resolved "https://registry.yarnpkg.com/@formio/text-mask-addons/-/text-mask-addons-3.8.0-formio.3.tgz#9da71371a2cb6f781e5e9f90246f25b3d053f75d"
integrity sha512-izNhggJTaCEJNOhtdCkouLxBP3byl+ObsHTdp9RpKwNz0k/ZIy/0xuelk1NY4vAjKojnJPiAjtxn+rrcZnEkpw==
"@formio/text-mask-addons@^3.8.0-formio.4":
version "3.8.0-formio.4"
resolved "https://registry.yarnpkg.com/@formio/text-mask-addons/-/text-mask-addons-3.8.0-formio.4.tgz#f3ce4bf978c6c5e8f671641d96f6fd116f7c92a3"
integrity sha512-vhkeIyuL+1rtC9S4IW8O3JCwroPtvJrkrcMO4wyELNqMIgQRKbiyBAitZfUP4tY04xdB5lxAinbzdwb+NMdX6w==

"@formio/vanilla-text-mask@^5.1.1-formio.1":
version "5.1.1-formio.1"
Expand Down
Loading