2021-10-24 21:53:44 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
2021-10-22 23:08:09 +00:00
|
|
|
class Utils {
|
2022-01-22 22:37:02 +00:00
|
|
|
static double roundToDecimalPlaces(double value, int precision) {
|
|
|
|
double mod = pow(10.0, precision).toDouble();
|
2021-10-24 21:53:44 +00:00
|
|
|
return ((value * mod).round().toDouble() / mod);
|
|
|
|
}
|
|
|
|
|
2022-01-22 22:37:02 +00:00
|
|
|
static double addDoublesWithPrecision(double a, double b, int precision) {
|
|
|
|
double mod = pow(10.0, precision).toDouble();
|
|
|
|
double difference = (a * mod) + (b * mod);
|
|
|
|
return difference.round() / mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getFractionDigitsLength(double value) {
|
|
|
|
final fractionDigits = value.toString().split('.');
|
|
|
|
return fractionDigits[1].length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static String toStringMatchingTemplateFractionPrecision(
|
|
|
|
double value, double template) {
|
|
|
|
final precision = getFractionDigitsLength(template);
|
|
|
|
return value.toStringAsFixed(precision);
|
|
|
|
}
|
|
|
|
|
2021-10-22 23:08:09 +00:00
|
|
|
static double convertMgPerDlToMmolPerL(int mgPerDl) {
|
2021-10-24 21:53:44 +00:00
|
|
|
return Utils.roundToDecimalPlaces(mgPerDl * 0.0555, 2);
|
2021-10-22 23:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int convertMmolPerLToMgPerDl(double mmolPerL) {
|
|
|
|
return (mmolPerL * 18.018).round();
|
|
|
|
}
|
2021-10-24 21:53:44 +00:00
|
|
|
|
2022-01-22 22:37:02 +00:00
|
|
|
static double calculateCarbs(double carbsRatio, double portionSize) {
|
2021-10-24 21:53:44 +00:00
|
|
|
return Utils.roundToDecimalPlaces(carbsRatio * portionSize / 100, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double calculateCarbsRatio(
|
|
|
|
double carbsPerPortion, double portionSize) {
|
2022-01-22 22:37:02 +00:00
|
|
|
return portionSize > 0
|
|
|
|
? Utils.roundToDecimalPlaces(carbsPerPortion * 100 / portionSize, 2)
|
|
|
|
: 0;
|
2021-10-24 21:53:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static double calculatePortionSize(
|
|
|
|
double carbsRatio, double carbsPerPortion) {
|
2022-01-22 22:37:02 +00:00
|
|
|
return carbsRatio > 0
|
|
|
|
? Utils.roundToDecimalPlaces(carbsPerPortion * 100 / carbsRatio, 2)
|
|
|
|
: 0;
|
2021-10-24 21:53:44 +00:00
|
|
|
}
|
2021-10-22 23:08:09 +00:00
|
|
|
}
|