Merge pull request #9632 from tchak/fix-decimal-bis

fix(decimal_champ): only remove dots if coma also present
This commit is contained in:
mfo 2023-10-23 17:03:50 +02:00 committed by GitHub
commit dbd0e82b09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 18 deletions

View file

@ -52,12 +52,19 @@ export class FormatController extends ApplicationController {
} }
private formatDecimal(value: string) { private formatDecimal(value: string) {
const decimalSeparator = getDecimalSeparator(value); let formattedNumber = value;
const number = const lastDotIndex = value.lastIndexOf('.');
decimalSeparator == ',' const lastCommaIndex = value.lastIndexOf(',');
? value.replace(/\./g, '').replace(/,/g, '.') if (lastDotIndex != -1 && lastCommaIndex != -1) {
: value.replace(/,/g, ''); if (lastDotIndex < lastCommaIndex) {
return number.replace(new RegExp(`[^-?\\d.]`, 'g'), ''); formattedNumber = value.replace(/\./g, '');
} else {
formattedNumber = value.replace(/,/g, '');
}
}
return formattedNumber
.replace(/,/g, '.')
.replace(new RegExp(`[^-?\\d.]`, 'g'), '');
} }
} }
@ -71,15 +78,3 @@ function replaceValue(target: HTMLInputElement, value: string) {
target.selectionEnd = end ? end - delta : 0; target.selectionEnd = end ? end - delta : 0;
target.selectionDirection = dir; target.selectionDirection = dir;
} }
function getDecimalSeparator(value: string) {
if (value.indexOf('.') != -1 && value.indexOf(',') != -1) {
if (value.lastIndexOf('.') < value.lastIndexOf(',')) {
return ',';
}
return '.';
} else if (value.indexOf(',') != -1) {
return ',';
}
return (1.1).toLocaleString().indexOf('.') != -1 ? '.' : ',';
}

View file

@ -205,6 +205,28 @@ describe 'The user' do
wait_until { wait_until {
champ_value_for('nombre décimal') == '123456.78' champ_value_for('nombre décimal') == '123456.78'
} }
champ_past_value_for('nombre décimal', '123 456,78')
wait_until {
champ_value_for('nombre décimal') == '123456.78'
}
fill_in('nombre décimal', with: '123 456.78')
wait_until {
champ_value_for('nombre décimal') == '123456.78'
}
champ_past_value_for('nombre décimal', '123 456.78')
wait_until {
champ_value_for('nombre décimal') == '123456.78'
}
fill_in('nombre décimal', with: '123 456.002')
wait_until {
champ_value_for('nombre décimal') == '123456.002'
}
fill_in('nombre décimal', with: '123 456,002')
wait_until {
champ_value_for('nombre décimal') == '123456.002'
}
champ_past_value_for('nombre décimal', '1,234.56') champ_past_value_for('nombre décimal', '1,234.56')
wait_until { wait_until {