2 decimal limit
This commit is contained in:
parent
3ca526e0f1
commit
91997ae251
1 changed files with 12 additions and 9 deletions
|
|
@ -2,6 +2,7 @@ package commons;
|
||||||
|
|
||||||
import jakarta.persistence.Entity;
|
import jakarta.persistence.Entity;
|
||||||
|
|
||||||
|
import java.text.DecimalFormat;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
|
@ -19,6 +20,8 @@ public class FormalIngredient extends RecipeIngredient implements Scalable<Forma
|
||||||
private static final int tbspToCupConvert = 16;
|
private static final int tbspToCupConvert = 16;
|
||||||
private static final int tbspToOunceConvert = 2;
|
private static final int tbspToOunceConvert = 2;
|
||||||
private static final int OunceToPoundConvert = 16;
|
private static final int OunceToPoundConvert = 16;
|
||||||
|
private static final DecimalFormat numberFormat = new DecimalFormat("#.00");
|
||||||
|
|
||||||
|
|
||||||
public double getAmount() {
|
public double getAmount() {
|
||||||
return amount;
|
return amount;
|
||||||
|
|
@ -66,7 +69,7 @@ public class FormalIngredient extends RecipeIngredient implements Scalable<Forma
|
||||||
public String normalisedUnit(){
|
public String normalisedUnit(){
|
||||||
Optional<Unit> unit = Unit.fromString(unitSuffix);
|
Optional<Unit> unit = Unit.fromString(unitSuffix);
|
||||||
if (unit.isEmpty() || !unit.get().isFormal() || unit.get().conversionFactor <= 0) {
|
if (unit.isEmpty() || !unit.get().isFormal() || unit.get().conversionFactor <= 0) {
|
||||||
return amount + unitSuffix;
|
return numberFormat.format(amount) + unitSuffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
Unit currentUnit = unit.get();
|
Unit currentUnit = unit.get();
|
||||||
|
|
@ -75,37 +78,37 @@ public class FormalIngredient extends RecipeIngredient implements Scalable<Forma
|
||||||
switch (currentUnit){
|
switch (currentUnit){
|
||||||
case GRAMME -> {
|
case GRAMME -> {
|
||||||
if(baseAmount >= Unit.TONNE.conversionFactor){
|
if(baseAmount >= Unit.TONNE.conversionFactor){
|
||||||
return baseAmount /Unit.TONNE.conversionFactor + Unit.TONNE.suffix;
|
return numberFormat.format(baseAmount /Unit.TONNE.conversionFactor) + Unit.TONNE.suffix;
|
||||||
}if(baseAmount >=Unit.KILOGRAMME.conversionFactor) {
|
}if(baseAmount >=Unit.KILOGRAMME.conversionFactor) {
|
||||||
return baseAmount / Unit.KILOGRAMME.conversionFactor + Unit.KILOGRAMME.suffix;
|
return numberFormat.format(baseAmount / Unit.KILOGRAMME.conversionFactor) + Unit.KILOGRAMME.suffix;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case MILLILITRE -> {
|
case MILLILITRE -> {
|
||||||
if (baseAmount >= Unit.LITRE.conversionFactor) {
|
if (baseAmount >= Unit.LITRE.conversionFactor) {
|
||||||
return baseAmount /Unit.LITRE.conversionFactor + Unit.LITRE.suffix;
|
return numberFormat.format(baseAmount /Unit.LITRE.conversionFactor) + Unit.LITRE.suffix;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case TABLESPOON -> {
|
case TABLESPOON -> {
|
||||||
if(amount>=tbspToPoundConvert){
|
if(amount>=tbspToPoundConvert){
|
||||||
return amount/tbspToPoundConvert + Unit.POUND.suffix;
|
return numberFormat.format(amount/tbspToPoundConvert) + Unit.POUND.suffix;
|
||||||
}
|
}
|
||||||
if(amount>=tbspToCupConvert){
|
if(amount>=tbspToCupConvert){
|
||||||
return amount /tbspToCupConvert + Unit.CUP.suffix;
|
return numberFormat.format(amount /tbspToCupConvert) + Unit.CUP.suffix;
|
||||||
}
|
}
|
||||||
if(amount>=tbspToOunceConvert){
|
if(amount>=tbspToOunceConvert){
|
||||||
return amount /tbspToOunceConvert + Unit.OUNCE.suffix;
|
return numberFormat.format(amount /tbspToOunceConvert) + Unit.OUNCE.suffix;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case OUNCE -> {
|
case OUNCE -> {
|
||||||
if (baseAmount >= OunceToPoundConvert) {
|
if (baseAmount >= OunceToPoundConvert) {
|
||||||
return amount / OunceToPoundConvert + Unit.POUND.suffix;
|
return numberFormat.format(amount / OunceToPoundConvert) + Unit.POUND.suffix;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return amount + currentUnit.suffix;
|
return numberFormat.format(amount) + currentUnit.suffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue