fix: rounding erros when balancing accounts

This commit is contained in:
Herculino Trotta
2024-11-24 18:58:18 -03:00
parent cb2de69ec2
commit f521fe9016

View File

@@ -25,7 +25,7 @@
<div class="form-label"> <div class="form-label">
{% translate 'Current balance' %} {% translate 'Current balance' %}
</div> </div>
<div data-amount="{{ form.current_balance }}" <div data-amount="{{ form.current_balance|floatformat:"-40u" }}"
data-decimal-places="{{ form.currency_decimal_places }}" data-decimal-places="{{ form.currency_decimal_places }}"
id="amount-{{ forloop.counter0 }}"> id="amount-{{ forloop.counter0 }}">
{% currency_display amount=form.current_balance prefix=form.currency_prefix suffix=form.currency_suffix decimal_places=form.currency_decimal_places %} {% currency_display amount=form.current_balance prefix=form.currency_prefix suffix=form.currency_suffix decimal_places=form.currency_decimal_places %}
@@ -39,24 +39,26 @@
{% translate 'Difference' %} {% translate 'Difference' %}
</div> </div>
<div _="on input from #id_form-{{ forloop.counter0 }}-new_balance <div _="on input from #id_form-{{ forloop.counter0 }}-new_balance
set original_amount to parseLocaleNumber('{{ form.current_balance }}') set original_amount to parseFloat('{{ form.current_balance|floatformat:"-40u" }}')
then call parseLocaleNumber(#id_form-{{ forloop.counter0 }}-new_balance.value)
then set new_amount to result
then set diff to new_amount - original_amount
then set prefix to '{{ form.currency_prefix }}' then set prefix to '{{ form.currency_prefix }}'
then set suffix to '{{ form.currency_suffix }}' then set suffix to '{{ form.currency_suffix }}'
then set decimal_places to {{ form.currency_decimal_places }} then set decimal_places to {{ form.currency_decimal_places }}
then call parseLocaleNumber(#id_form-{{ forloop.counter0 }}-new_balance.value)
then set new_amount to result
then set diff to (Math.round((new_amount - original_amount) * Math.pow(10, decimal_places))) / Math.pow(10, decimal_places)
then log diff
then set format_new_amount to then set format_new_amount to
Intl.NumberFormat( Intl.NumberFormat(
undefined, undefined,
{ {
minimumFractionDigits: decimal_places, minimumFractionDigits: decimal_places,
maximumFractionDigits: decimal_places, maximumFractionDigits: decimal_places,
roundingMode: 'trunc'} roundingMode: 'trunc'
}
).format(diff) ).format(diff)
then set formatted_string to `${prefix}${format_new_amount}${suffix}` then set formatted_string to `${prefix}${format_new_amount}${suffix}`
then put formatted_string into me if diff else then put formatted_string into me if diff else
put '' into me">-</div> put '-' into me">-</div>
</div> </div>
</div> </div>
</div> </div>