2021-10-24 21:53:44 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
2021-10-22 23:08:09 +00:00
|
|
|
class Utils {
|
2022-01-24 04:43:55 +00:00
|
|
|
// static double roundToDecimalPlaces(double value, int precision) {
|
|
|
|
// double mod = pow(10.0, precision).toDouble();
|
|
|
|
// return ((value * mod).round().toDouble() / mod);
|
|
|
|
// }
|
|
|
|
|
|
|
|
static double roundToMultipleOfBase(double value, double base) {
|
|
|
|
double result = value;
|
|
|
|
double remainder = value % base;
|
|
|
|
int precision = Utils.getFractionDigitsLength(base);
|
|
|
|
|
|
|
|
if (remainder != 0) {
|
|
|
|
result = Utils.addDoublesWithPrecision(result, -remainder, precision);
|
|
|
|
if (remainder > base / 2) {
|
|
|
|
result = Utils.addDoublesWithPrecision(result, base, precision);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2021-10-24 21:53:44 +00:00
|
|
|
}
|
|
|
|
|
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('.');
|
2022-01-24 04:43:55 +00:00
|
|
|
return fractionDigits[1] == '0' ? 0 : fractionDigits[1].length;
|
2022-01-22 22:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static String toStringMatchingTemplateFractionPrecision(
|
|
|
|
double value, double template) {
|
|
|
|
final precision = getFractionDigitsLength(template);
|
|
|
|
return value.toStringAsFixed(precision);
|
|
|
|
}
|
|
|
|
|
2022-01-24 04:43:55 +00:00
|
|
|
static double convertMgPerDlToMmolPerL(int mgPerDl, {double step = 0.01}) {
|
|
|
|
return Utils.roundToMultipleOfBase(mgPerDl * 0.0555, step);
|
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-24 04:43:55 +00:00
|
|
|
static double calculateCarbs(double carbsRatio, double portionSize,
|
|
|
|
{double step = 0.01}) {
|
|
|
|
return Utils.roundToMultipleOfBase(carbsRatio * portionSize / 100, step);
|
2021-10-24 21:53:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static double calculateCarbsRatio(
|
2022-01-24 04:43:55 +00:00
|
|
|
double carbsPerPortion, double portionSize, {double step = 0.01}) {
|
2022-01-22 22:37:02 +00:00
|
|
|
return portionSize > 0
|
2022-01-24 04:43:55 +00:00
|
|
|
? Utils.roundToMultipleOfBase(carbsPerPortion * 100 / portionSize, step)
|
2022-01-22 22:37:02 +00:00
|
|
|
: 0;
|
2021-10-24 21:53:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static double calculatePortionSize(
|
2022-01-24 04:43:55 +00:00
|
|
|
double carbsRatio, double carbsPerPortion, {double step = 0.01}) {
|
2022-01-22 22:37:02 +00:00
|
|
|
return carbsRatio > 0
|
2022-01-24 04:43:55 +00:00
|
|
|
? Utils.roundToMultipleOfBase(carbsPerPortion * 100 / carbsRatio, step)
|
2022-01-22 22:37:02 +00:00
|
|
|
: 0;
|
2021-10-24 21:53:44 +00:00
|
|
|
}
|
2021-10-22 23:08:09 +00:00
|
|
|
}
|