From 10ba8b5e46f378244f2fc316e82b736fbc0e61be Mon Sep 17 00:00:00 2001 From: vjrj <vjrj@comunes.org> Date: Wed, 19 Apr 2023 20:55:54 +0200 Subject: [PATCH] Improve calculs in payments --- lib/ui/ui_helpers.dart | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/ui/ui_helpers.dart b/lib/ui/ui_helpers.dart index 26c619b0..d5373b0b 100644 --- a/lib/ui/ui_helpers.dart +++ b/lib/ui/ui_helpers.dart @@ -144,8 +144,9 @@ String formatAmountWithLocale(String locale, double amount) { String formatKAmount(BuildContext context, double amount) => formatAmount(context, amount / 100); -double parseToDoubleLocalized(String locale, String double) => - NumberFormat.decimalPattern(locale).parse(double).toDouble(); +double parseToDoubleLocalized( + {required String locale, required String number}) => + NumberFormat.decimalPattern(locale).parse(number).toDouble(); String localizeNumber(BuildContext context, double amount) => NumberFormat.decimalPattern(context.locale.toString()).format(amount); @@ -241,3 +242,37 @@ ImageIcon get g1nkgoIcon => ImageIcon( String get ginkgoIconLocation => assets('img/favicon.png'); String capitalize(String s) => s[0].toUpperCase() + s.substring(1); + +double calculate({required String textInTerminal, required String decimalSep}) { + String operation = textInTerminal; + double sum = 0.0; + operation = operation.replaceAll( + decimalSep, '.'); // change decimal separator to a dot + final RegExp regex = RegExp(r'[\d.]+'); // regular expression to find numbers + final Iterable<Match> matches = + regex.allMatches(operation); // find all numbers in the input + for (final Match? match in matches) { + try { + if (match != null) { + final String? g1 = match.group(0); + if (g1 != null) { + sum += double.parse(g1); // add the number to the sum + } + } + } catch (e) { + // could not convert the number to a double value, ignore it + } + } + // logger(numberFormat.format(sum)); // print the formatted sum + return sum; +} + +String decimalSep(BuildContext context) { + return NumberFormat.decimalPattern(context.locale.toString()) + .symbols + .DECIMAL_SEP; +} + +Color selectedPatternLock(BuildContext context) => Colors.red; + +Color notSelectedPatternLock(BuildContext context) => Colors.amber; -- GitLab