ingredient rounding upgrades

This commit is contained in:
vabene1111
2020-06-11 22:32:45 +02:00
parent 8cc0fcaed2
commit 8ff52f542e
6 changed files with 54 additions and 8 deletions

View File

@@ -327,6 +327,8 @@
<script type="text/javascript">
reloadIngredients()
$(function () {
$('[data-toggle="popover"]').popover()
});
@@ -335,20 +337,25 @@
trigger: 'focus'
});
function roundToTwo(num) {
return +(Math.round(num + "e+2") + "e-2");
function roundDecimals(num) {
return +(Math.round(num + "e+{{ request.user.userpreference.ingredient_decimals }}") + "e-{{ request.user.userpreference.ingredient_decimals }}");
}
function reloadIngredients() {
factor = Number($('#in_factor').val());
ingredients = {
let factor = Number($('#in_factor').val());
let ingredients = {
{% for i in ingredients %}
{{ i.pk }}: {{ i.amount|unlocalize }},
{% endfor %}
}
for (var key in ingredients) {
$('#ing_' + key).html(roundToTwo(ingredients[key] * factor))
for (let key in ingredients) {
let val = ''
if (Math.abs(ingredients[key] * factor - roundDecimals(ingredients[key] * factor)) > 0) {
val += '~'
}
$('#ing_' + key).html(val + roundDecimals(ingredients[key] * factor))
}
}