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

FIO-9271: Scientific notation in number component. #5926

Open
wants to merge 1 commit into
base: 4.21.x
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
21 changes: 17 additions & 4 deletions src/components/number/Number.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,23 @@ export default class NumberComponent extends Input {
if (typeof input === 'string') {
input = input.split(this.delimiter).join('').replace(this.decimalSeparator, '.');
}
let value = parseFloat(input);

if (!_.isNaN(value)) {
value = String(value).replace('.', this.decimalSeparator);
let value;

if (!_.isNaN(input)) {
// Format scientific notation
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 = parseFloat(input);
value = !_.isNaN(value) ? String(value).replace('.', this.decimalSeparator) : null;
}
}
else {
value = null;
Expand Down
21 changes: 20 additions & 1 deletion src/components/number/Number.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
comp6,
comp7,
comp8,
comp9
comp9,
scientificNotation
} from './fixtures';
import CurrencyComponent from '../currency/Currency';

Expand All @@ -25,6 +26,24 @@ describe('Number Component', () => {
});
});

it('Should correctly handle scientific notation', () => {
return Harness.testCreate(NumberComponent, scientificNotation).then((component) => {
const testCases = [
['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}`);
});
});
});

it('Should format submissions for table view for French locale', () => {
return Harness.testCreate(NumberComponent, comp4, { language: 'fr' }).then((component) => {
const value1 = component.getValueAsString(1);
Expand Down
13 changes: 13 additions & 0 deletions src/components/number/fixtures/scientificNotation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default {
'label': 'Number',
'mask': false,
'spellcheck': true,
'tableView': true,
'delimiter': true,
'requireDecimal': false,
'inputFormat': 'plain',
'key': 'number',
'type': 'number',
'decimalLimit': 2,
'input': true
};