[FIX] web: properly parse float with '.' as thousand_sep
In a slightly too naive way, we apparently tried to improve the parse_float method while rewriting the new views. This was done by using a regexp to match all occurences of a thousand separator. The way the regexp was built was fine, until the thousand separator is a '.', which, if used as a regexp, matches all characters. Also, sadly, '.' is not so rare as a thousand separator...
This commit is contained in:
@@ -415,7 +415,10 @@ function parseDateTime(value, field, options) {
|
||||
}
|
||||
|
||||
function parseFloat(value) {
|
||||
value = value.replace(new RegExp(core._t.database.parameters.thousands_sep, "g"), '');
|
||||
if (core._t.database.parameters.thousands_sep) {
|
||||
var escapedSep = _.str.escapeRegExp(core._t.database.parameters.thousands_sep);
|
||||
value = value.replace(new RegExp(escapedSep, 'g'), '');
|
||||
}
|
||||
value = value.replace(core._t.database.parameters.decimal_point, '.');
|
||||
var parsed = Number(value);
|
||||
if (isNaN(parsed)) {
|
||||
|
||||
@@ -101,7 +101,7 @@ QUnit.test('format one2many', function(assert) {
|
||||
});
|
||||
|
||||
QUnit.test('parse float', function(assert) {
|
||||
assert.expect(6);
|
||||
assert.expect(7);
|
||||
|
||||
assert.strictEqual(fieldUtils.parse.float(""), 0);
|
||||
assert.strictEqual(fieldUtils.parse.float("0"), 0);
|
||||
@@ -109,6 +109,16 @@ QUnit.test('parse float', function(assert) {
|
||||
assert.strictEqual(fieldUtils.parse.float("-100.00"), -100);
|
||||
assert.strictEqual(fieldUtils.parse.float("1,000.00"), 1000);
|
||||
assert.strictEqual(fieldUtils.parse.float("1,000,000.00"), 1000000);
|
||||
|
||||
var originalParameters = $.extend(true, {}, core._t.database.parameters);
|
||||
_.extend(core._t.database.parameters, {
|
||||
grouping: [3, 0],
|
||||
decimal_point: ',',
|
||||
thousands_sep: '.'
|
||||
});
|
||||
assert.strictEqual(fieldUtils.parse.float('1.234,567'), 1234.567);
|
||||
|
||||
core._t.database.parameters = originalParameters;
|
||||
});
|
||||
|
||||
QUnit.test('parse monetary', function(assert) {
|
||||
|
||||
Reference in New Issue
Block a user