Skip to content

Commit ae432ab

Browse files
committed
[FIX] core: Fix float_repr/formatFloat displaying -0.0
float_repr(-0.00000001, 2) formatFloat(-0.00000001, { digits: [16, 2] }) Before: "-0.00" After: "0.00" opw-4685953 closes odoo#220650 X-original-commit: ce156c3 Related: odoo/enterprise#91061 Signed-off-by: Claire Bretton (clbr) <clbr@odoo.com> Signed-off-by: Laurent Smet (las) <las@odoo.com>
1 parent f3c971e commit ae432ab

6 files changed

Lines changed: 22 additions & 13 deletions

File tree

addons/l10n_es_edi_facturae/tests/data/expected_refund_document.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
<TotalGrossAmount>-200.00</TotalGrossAmount>
110110
<TotalGrossAmountBeforeTaxes>-200.00</TotalGrossAmountBeforeTaxes>
111111
<TotalTaxOutputs>-42.00</TotalTaxOutputs>
112-
<TotalTaxesWithheld>-0.00</TotalTaxesWithheld>
112+
<TotalTaxesWithheld>0.00</TotalTaxesWithheld>
113113
<InvoiceTotal>-242.00</InvoiceTotal>
114114
<TotalOutstandingAmount>-242.00</TotalOutstandingAmount>
115115
<TotalExecutableAmount>-242.00</TotalExecutableAmount>

addons/sale/tests/test_taxes_downpayment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ def test_down_payment_100_first_then_0_final_invoice_round_globally(self):
830830
'amount_total': 0.0,
831831
}])
832832
self.assert_invoice_tax_totals_summary(final_invoice, {
833-
'same_tax_base': False, # <- This is a bug because we end up with 0.0 & -0.0 as base. Remove this comment when fixed.
833+
'same_tax_base': True,
834834
'currency_id': other_currency.id,
835835
'company_currency_id': self.env.company.currency_id.id,
836836
'base_amount_currency': 0.0,

addons/web/static/src/core/utils/numbers.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,21 @@ export function humanNumber(number, options = { decimals: 0, minDigits: 1 }) {
211211
* @returns {string}
212212
*/
213213
export function formatFloat(value, options = {}) {
214-
if (options.humanReadable) {
215-
return humanNumber(value, options);
216-
}
217-
const grouping = options.grouping || l10n.grouping;
218-
const thousandsSep = "thousandsSep" in options ? options.thousandsSep : l10n.thousandsSep;
219-
const decimalPoint = "decimalPoint" in options ? options.decimalPoint : l10n.decimalPoint;
220214
let precision;
221215
if (options.digits && options.digits[1] !== undefined) {
222216
precision = options.digits[1];
223217
} else {
224218
precision = 2;
225219
}
220+
if (floatIsZero(value, precision)) {
221+
value = 0.0;
222+
}
223+
if (options.humanReadable) {
224+
return humanNumber(value, options);
225+
}
226+
const grouping = options.grouping || l10n.grouping;
227+
const thousandsSep = "thousandsSep" in options ? options.thousandsSep : l10n.thousandsSep;
228+
const decimalPoint = "decimalPoint" in options ? options.decimalPoint : l10n.decimalPoint;
226229
const formatted = value.toFixed(precision).split(".");
227230
formatted[0] = insertThousandsSep(formatted[0], thousandsSep, grouping);
228231
if (options.trailingZeros === false && formatted[1]) {

addons/web/static/tests/core/utils/numbers.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ describe("formatFloat", () => {
317317
expect(formatFloat(-1e21, options)).toBe("-1e+21");
318318
expect(formatFloat(-1.0045e22, options)).toBe("-1e+22");
319319
expect(formatFloat(-1.012e43, options)).toBe("-1.01e+43");
320+
expect(formatFloat(-0.0000001, options)).toBe("0.00");
320321

321322
Object.assign(options, { decimals: 2, minDigits: 2 });
322323
expect(formatFloat(1020000, options)).toBe("1,020k");
@@ -329,5 +330,8 @@ describe("formatFloat", () => {
329330
Object.assign(options, { decimals: 3, minDigits: 1 });
330331
expect(formatFloat(1.0045e22, options)).toBe("1.005e+22");
331332
expect(formatFloat(-1.0045e22, options)).toBe("-1.004e+22");
333+
334+
Object.assign(options, { humanReadable: false });
335+
expect(formatFloat(-0.0000001, options)).toBe("0.00");
332336
});
333337
});

odoo/addons/base/tests/test_float.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def try_round(amount, expected, digits=2, method='HALF-UP'):
2222
try_round(2.675,'2.68') # in Python 2.7.2, round(2.675,2) gives 2.67
2323
try_round(-2.675,'-2.68') # in Python 2.7.2, round(2.675,2) gives 2.67
2424
try_round(0.001,'0.00')
25-
try_round(-0.001,'-0.00')
25+
try_round(-0.001, '0.00')
2626
try_round(0.0049,'0.00') # 0.0049 is closer to 0 than to 0.01, so should round down
2727
try_round(0.005,'0.01') # the rule is to round half away from zero
2828
try_round(-0.005,'-0.01') # the rule is to round half away from zero
@@ -81,7 +81,7 @@ def try_round(amount, expected, digits=3, method='HALF-UP'):
8181
try_round(2.6744, '2.674')
8282
try_round(-2.6744, '-2.674')
8383
try_round(0.0004, '0.000')
84-
try_round(-0.0004, '-0.000')
84+
try_round(-0.0004, '0.000')
8585
try_round(357.4555, '357.456')
8686
try_round(-357.4555, '-357.456')
8787
try_round(457.4554, '457.455')
@@ -95,7 +95,7 @@ def try_round(amount, expected, digits=3, method='HALF-UP'):
9595
try_round(2.6744, '2.674', method='HALF-DOWN')
9696
try_round(-2.6744, '-2.674', method='HALF-DOWN')
9797
try_round(0.0004, '0.000', method='HALF-DOWN')
98-
try_round(-0.0004, '-0.000', method='HALF-DOWN')
98+
try_round(-0.0004, '0.000', method='HALF-DOWN')
9999
try_round(357.4555, '357.455', method='HALF-DOWN')
100100
try_round(-357.4555, '-357.455', method='HALF-DOWN')
101101
try_round(457.4554, '457.455', method='HALF-DOWN')
@@ -109,7 +109,7 @@ def try_round(amount, expected, digits=3, method='HALF-UP'):
109109
try_round(2.6744, '2.674', method='HALF-EVEN')
110110
try_round(-2.6744, '-2.674', method='HALF-EVEN')
111111
try_round(0.0004, '0.000', method='HALF-EVEN')
112-
try_round(-0.0004, '-0.000', method='HALF-EVEN')
112+
try_round(-0.0004, '0.000', method='HALF-EVEN')
113113
try_round(357.4555, '357.456', method='HALF-EVEN')
114114
try_round(-357.4555, '-357.456', method='HALF-EVEN')
115115
try_round(457.4554, '457.455', method='HALF-EVEN')
@@ -228,7 +228,7 @@ def try_split(value, expected, split_fun, rounding=None):
228228
try_split(2.675, ('2', '68'), float_split_str) # in Python 2.7.2, round(2.675,2) gives 2.67
229229
try_split(-2.675, ('-2', '68'), float_split_str) # in Python 2.7.2, round(2.675,2) gives 2.67
230230
try_split(0.001, ('0', '00'), float_split_str)
231-
try_split(-0.001, ('-0', '00'), float_split_str)
231+
try_split(-0.001, ('0', '00'), float_split_str)
232232
try_split(42, ('42', '00'), float_split_str)
233233
try_split(0.1, ('0', '10'), float_split_str)
234234
try_split(13.0, ('13', ''), float_split_str, rounding=0)

odoo/tools/float_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ def float_repr(value, precision_digits):
205205
# Can't use str() here because it seems to have an intrinsic
206206
# rounding to 12 significant digits, which causes a loss of
207207
# precision. e.g. str(123456789.1234) == str(123456789.123)!!
208+
if float_is_zero(value, precision_digits=precision_digits):
209+
value = 0.0
208210
return "%.*f" % (precision_digits, value)
209211

210212

0 commit comments

Comments
 (0)