Skip to content

Commit

Permalink
fix convert to scientific notation function
Browse files Browse the repository at this point in the history
  • Loading branch information
mirpedrol committed Mar 19, 2024
1 parent fc4e162 commit 5e3fb9d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,16 @@ class CO2FootprintFactory implements TraceObserverFactory {
car = gCO2 / 251 as Double
}
Double tree = gCO2 / 917 as Double
car = car.round(2)
tree = tree.round(2)
car = car
tree = tree
Double plane_percent
Double plane_flights
if (gCO2 <= 50000) {
plane_percent = gCO2 * 100 / 50000 as Double
plane_percent = plane_percent.round(2)
plane_percent = plane_percent
} else {
plane_flights = gCO2 / 50000 as Double
plane_flights = plane_flights.round(2)
plane_flights = plane_flights
}

return [car, tree, plane_percent, plane_flights]
Expand Down Expand Up @@ -748,10 +748,10 @@ class CO2FootprintFactory implements TraceObserverFactory {
List equivalences = computeCO2footprintEquivalences()
[ co2:HelperFunctions.convertToReadableUnits(total_co2,3),
energy:HelperFunctions.convertToReadableUnits(total_energy,3),
car: equivalences[0]?HelperFunctions.convertToScientificNotation(equivalences[0]):null,
tree: equivalences[1]?HelperFunctions.convertToScientificNotation(equivalences[1]):null,
plane_percent: equivalences[2]?HelperFunctions.convertToScientificNotation(equivalences[2]):null,
plane_flights: equivalences[3]?HelperFunctions.convertToScientificNotation(equivalences[3]):null
car: equivalences[0]?HelperFunctions.convertToScientificNotation(equivalences[0]):equivalences[0],
tree: equivalences[1]?HelperFunctions.convertToScientificNotation(equivalences[1]):equivalences[1],
plane_percent: equivalences[2]?HelperFunctions.convertToScientificNotation(equivalences[2]):equivalences[2],
plane_flights: equivalences[3]?HelperFunctions.convertToScientificNotation(equivalences[3]):equivalences[3]
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ public class HelperFunctions {
if (value == 0) {
return value.toString()
} else if (value == null) {
return null
return value
}
return String.format('%e', value)
String scientific
Integer intPart = (Integer) value
Integer exponential = 0
while (value < 1 && value - intPart != 0 ) {
value = value * 10
exponential++
}
scientific = (String) value.round(2) + "e-" + exponential
return scientific
}

static public String convertToReadableUnits(double value, int unitIndex=4) {
Expand Down

0 comments on commit 5e3fb9d

Please sign in to comment.