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-9173: Allow Scientific Notations #5878

Merged
merged 4 commits into from
Oct 28, 2024
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
7 changes: 6 additions & 1 deletion src/components/number/Number.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,12 @@ export default class NumberComponent extends Input {
let value = parseFloat(input);

if (!_.isNaN(value)) {
value = String(value).replace('.', this.decimalSeparator);
// Format scientific notation
if (/e/i.test(String(value))) {
value = value.toExponential(this.decimalLimit);
} else {
value = String(value).replace('.', this.decimalSeparator);
}
}
else {
value = null;
Expand Down
23 changes: 22 additions & 1 deletion test/unit/Number.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
comp8,
comp9,
comp10,
comp11
comp11,
scientificNotation
} from './fixtures/number';

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

it('Should correctly handle scientific notation', () => {
return Harness.testCreate(NumberComponent, scientificNotation, { allowScientificNotation: true }).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'],
];

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
2 changes: 1 addition & 1 deletion test/unit/fixtures/number/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ import comp8 from './comp8';
import comp9 from './comp9';
import comp10 from './comp10';
import comp11 from './comp11';
export { comp1, comp2, comp3, comp4, comp5, comp6, comp7, comp8, comp9, comp10, comp11 };
export { comp1, comp2, comp3, comp4, comp5, comp6, comp7, comp8, comp9, comp10, comp11, scientificNotation };
13 changes: 13 additions & 0 deletions test/unit/fixtures/number/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': true,
'inputFormat': 'plain',
'key': 'number',
'type': 'number',
'decimalLimit': 2,
'input': true
};
Loading