Skip to content

Commit

Permalink
Fixed rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
jlucaspains committed Aug 18, 2023
1 parent 8948292 commit f31fd60
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/helpers/multiplierHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ export function prepareIngredientDisplay(input: string, multiplier: number, useF
.filter(item => item.quantity > 0.1)
.map(item => {
return {
quantity: item.quantity * multiplier, unit: item.unit, unitText: item.unitText,
minQuantity: item.minQuantity * multiplier, maxQuantity: item.maxQuantity * multiplier
quantity: round(item.quantity * multiplier, 2), unit: item.unit, unitText: item.unitText,
minQuantity: round(item.minQuantity * multiplier, 2), maxQuantity: round(item.maxQuantity * multiplier, 2)
};
});
let newValueText = result.quantityText;
Expand All @@ -97,7 +97,7 @@ export function prepareIngredientDisplay(input: string, multiplier: number, useF

return {
text: displayText, quantityValue: newValue,
minQuantity: newMinQuantity, maxQuantity: newMaxQuantity,
minQuantity: round(newMinQuantity, 2), maxQuantity: round(newMaxQuantity, 2),
quantityText: newValueText, unit: result.unit, unitText: result.unitText,
ingredient: result.ingredient, alternativeQuantities: newAlternativeQuantities
};
Expand All @@ -124,4 +124,10 @@ export function prepareStepDisplay(input: string, currentTime: Date, locale: str
}

return { text: displayText, startTime: new Date(currentTime), timeInSeconds: result.totalTimeInSeconds, temperature: result.temperature, temperatureUnit: result.temperatureUnit };
}

function round(value: number, decimalPlaces = 0) {
var p = Math.pow(10, decimalPlaces);
var n = (value * p) * (1 + Number.EPSILON);
return Math.round(n) / p;
}

0 comments on commit f31fd60

Please sign in to comment.