From 20b8f4c7e8d99a7cdc2be934911c75c1da581eba Mon Sep 17 00:00:00 2001 From: FrancoisGe Date: Mon, 19 Sep 2022 16:37:00 +0000 Subject: [PATCH] [FIX] web: rounding error in percentageField Before this commit, some values entered in the percentage field could be changed due to a rounding error. For example, the value 28 was replaced by 28.000000000000004 How to reproduce it: - Go to a form view with percentage fields - Enter the value 28 in the percentage field Before this commit: The value in the input was replaced by 28.000000000000004 After this commit: The value in the input remains 28 closes odoo/odoo#100559 Signed-off-by: Aaron Bohy (aab) --- .../fields/percentage/percentage_field.js | 6 +++++- .../views/fields/percentage_field_tests.js | 20 ++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/addons/web/static/src/views/fields/percentage/percentage_field.js b/addons/web/static/src/views/fields/percentage/percentage_field.js index 23283c64b42..4579fd76da3 100644 --- a/addons/web/static/src/views/fields/percentage/percentage_field.js +++ b/addons/web/static/src/views/fields/percentage/percentage_field.js @@ -13,7 +13,11 @@ const { Component } = owl; export class PercentageField extends Component { setup() { useInputField({ - getValue: () => this.props.value * 100, + getValue: () => + formatPercentage(this.props.value, { + digits: this.props.digits, + noSymbol: true, + }), refName: "numpadDecimal", parse: (v) => parsePercentage(v), }); diff --git a/addons/web/static/tests/views/fields/percentage_field_tests.js b/addons/web/static/tests/views/fields/percentage_field_tests.js index be247ae8ea0..7035b8ecb7b 100644 --- a/addons/web/static/tests/views/fields/percentage_field_tests.js +++ b/addons/web/static/tests/views/fields/percentage_field_tests.js @@ -59,7 +59,7 @@ QUnit.module("Fields", (hooks) => { await click(target.querySelector(".o_form_button_edit")); assert.strictEqual( target.querySelector(".o_field_widget[name=float_field] input").value, - "44.444", + "44.4", "The input should be rendered without the percentage symbol." ); assert.strictEqual( @@ -67,8 +67,8 @@ QUnit.module("Fields", (hooks) => { "%", "The input should be followed by a span containing the percentage symbol." ); - const field = target.querySelector("input"); - await editInput(target, "input", "24"); + const field = target.querySelector("[name='float_field'] input"); + await editInput(target, "[name='float_field'] input", "24"); assert.strictEqual(field.value, "24", "The value should not be formated yet."); await click(target.querySelector(".o_form_button_save")); assert.strictEqual( @@ -97,4 +97,18 @@ QUnit.module("Fields", (hooks) => { "Placeholder" ); }); + + QUnit.test("PercentageField in form view without rounding error", async function (assert) { + await makeView({ + serverData, + type: "form", + resModel: "partner", + arch: ` +
+ + `, + }); + await editInput(target, "[name='float_field'] input", "28"); + assert.strictEqual(target.querySelector("[name='float_field'] input").value, "28"); + }); });