Skip to content

Commit

Permalink
bug fix on number parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
emavgl committed Mar 3, 2024
1 parent 8a84131 commit e7d7116
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 90 deletions.
167 changes: 82 additions & 85 deletions lib/helpers/records-utility-functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,31 @@ bool localeExists(String? localeName) {
}

String getLocaleGroupingSeparator() {
String myLocale = I18n.locale.toString();
String? existingLocale = helpers.verifiedLocale(myLocale, localeExists, null);
if (existingLocale == null) {
return ",";
}
return numberFormatSymbols[existingLocale]?.GROUP_SEP;
String existingCurrencyLocale = ServiceConfig.currencyLocale.toString();
NumberFormat currencyLocaleNumberFormat = new NumberFormat.currency(locale: existingCurrencyLocale);
return currencyLocaleNumberFormat.symbols.GROUP_SEP;
}

String getLocaleDecimalSeparator() {
String existingCurrencyLocale = ServiceConfig.currencyLocale.toString();
NumberFormat currencyLocaleNumberFormat = new NumberFormat.currency(locale: existingCurrencyLocale);
return currencyLocaleNumberFormat.symbols.DECIMAL_SEP;
}

String? getUserDefinedGroupingSeparator() {
return ServiceConfig.sharedPreferences!.getString("groupSeparator");
}

String getGroupingSeparator() {
return ServiceConfig.sharedPreferences!.getString("groupSeparator") ??
String s = ServiceConfig.sharedPreferences!.getString("groupSeparator") ??
getLocaleGroupingSeparator();
return s;
}

String getDecimalSeparator() {
return ServiceConfig.sharedPreferences!.getString("decimalSeparator") ??
String s = ServiceConfig.sharedPreferences!.getString("decimalSeparator") ??
getLocaleDecimalSeparator();
}

String getLocaleDecimalSeparator() {
String myLocale = I18n.locale.toString();
String? existingLocale = helpers.verifiedLocale(myLocale, localeExists, null);
if (existingLocale == null) {
return ".";
}
return numberFormatSymbols[existingLocale]?.DECIMAL_SEP;
return s;
}

bool getOverwriteDotValue() {
Expand All @@ -86,90 +82,91 @@ bool usesWesternArabicNumerals(Locale locale) {

NumberFormat getNumberFormatWithCustomizations(
{turnOffGrouping = false, locale}) {
NumberFormat? numberFormat = ServiceConfig.currencyNumberFormat;
NumberFormat? numberFormat;

if (numberFormat == null) {
String? userDefinedGroupSeparator =
ServiceConfig.sharedPreferences?.getString("groupSeparator");
int decimalDigits =
ServiceConfig.sharedPreferences?.getInt("numDecimalDigits") ?? 2;
String? userDefinedGroupSeparator =
ServiceConfig.sharedPreferences?.getString("groupSeparator");
int decimalDigits =
ServiceConfig.sharedPreferences?.getInt("numDecimalDigits") ?? 2;

try {
if (locale == null) {
locale = getCurrencyLocale();
}

NumberFormat referenceNumberFormat = new NumberFormat.currency(
locale: locale.toString(), symbol: "", decimalDigits: decimalDigits);

numberFormatSymbols['custom_locale'] = new NumberSymbols(
NAME: "c",
DECIMAL_SEP: getDecimalSeparator(),
GROUP_SEP: getGroupingSeparator(),
PERCENT: referenceNumberFormat.symbols.PERCENT,
ZERO_DIGIT: referenceNumberFormat.symbols.ZERO_DIGIT,
PLUS_SIGN: referenceNumberFormat.symbols.PLUS_SIGN,
MINUS_SIGN: referenceNumberFormat.symbols.MINUS_SIGN,
EXP_SYMBOL: referenceNumberFormat.symbols.EXP_SYMBOL,
PERMILL: referenceNumberFormat.symbols.PERMILL,
INFINITY: referenceNumberFormat.symbols.INFINITY,
NAN: referenceNumberFormat.symbols.NAN,
DECIMAL_PATTERN: referenceNumberFormat.symbols.DECIMAL_PATTERN,
SCIENTIFIC_PATTERN: referenceNumberFormat.symbols.SCIENTIFIC_PATTERN,
PERCENT_PATTERN: referenceNumberFormat.symbols.PERCENT_PATTERN,
CURRENCY_PATTERN: referenceNumberFormat.symbols.CURRENCY_PATTERN,
DEF_CURRENCY_CODE: referenceNumberFormat.symbols.DEF_CURRENCY_CODE);

numberFormat = new NumberFormat.currency(
locale: "custom_locale", symbol: "", decimalDigits: decimalDigits);

// Copy over some properties
numberFormat.maximumIntegerDigits =
referenceNumberFormat.maximumIntegerDigits;
numberFormat.minimumIntegerDigits =
referenceNumberFormat.minimumIntegerDigits;

numberFormat.minimumExponentDigits =
referenceNumberFormat.minimumExponentDigits;

numberFormat.maximumFractionDigits =
referenceNumberFormat.maximumFractionDigits;
numberFormat.minimumFractionDigits =
referenceNumberFormat.minimumFractionDigits;

numberFormat.maximumSignificantDigits =
referenceNumberFormat.maximumSignificantDigits;
numberFormat.minimumSignificantDigits =
referenceNumberFormat.minimumSignificantDigits;
} on Exception catch (_) {
numberFormat = new NumberFormat.currency(
locale: "en_US", symbol: "", decimalDigits: decimalDigits);
}
try {

bool mustRemoveGrouping = (userDefinedGroupSeparator != null &&
userDefinedGroupSeparator.isEmpty) ||
turnOffGrouping;
if (mustRemoveGrouping) {
numberFormat.turnOffGrouping();
if (locale == null) {
locale = getCurrencyLocale();
}

ServiceConfig.currencyNumberFormat = numberFormat;
NumberFormat referenceNumberFormat = new NumberFormat.currency(
locale: locale.toString(), symbol: "", decimalDigits: decimalDigits);

numberFormatSymbols['custom_locale'] = new NumberSymbols(
NAME: "c",
DECIMAL_SEP: getDecimalSeparator(),
GROUP_SEP: getGroupingSeparator(),
PERCENT: referenceNumberFormat.symbols.PERCENT,
ZERO_DIGIT: referenceNumberFormat.symbols.ZERO_DIGIT,
PLUS_SIGN: referenceNumberFormat.symbols.PLUS_SIGN,
MINUS_SIGN: referenceNumberFormat.symbols.MINUS_SIGN,
EXP_SYMBOL: referenceNumberFormat.symbols.EXP_SYMBOL,
PERMILL: referenceNumberFormat.symbols.PERMILL,
INFINITY: referenceNumberFormat.symbols.INFINITY,
NAN: referenceNumberFormat.symbols.NAN,
DECIMAL_PATTERN: referenceNumberFormat.symbols.DECIMAL_PATTERN,
SCIENTIFIC_PATTERN: referenceNumberFormat.symbols.SCIENTIFIC_PATTERN,
PERCENT_PATTERN: referenceNumberFormat.symbols.PERCENT_PATTERN,
CURRENCY_PATTERN: referenceNumberFormat.symbols.CURRENCY_PATTERN,
DEF_CURRENCY_CODE: referenceNumberFormat.symbols.DEF_CURRENCY_CODE);

numberFormat = new NumberFormat.currency(
locale: "custom_locale", symbol: "", decimalDigits: decimalDigits);

// Copy over some properties
numberFormat.maximumIntegerDigits =
referenceNumberFormat.maximumIntegerDigits;
numberFormat.minimumIntegerDigits =
referenceNumberFormat.minimumIntegerDigits;

numberFormat.minimumExponentDigits =
referenceNumberFormat.minimumExponentDigits;

numberFormat.maximumFractionDigits =
referenceNumberFormat.maximumFractionDigits;
numberFormat.minimumFractionDigits =
referenceNumberFormat.minimumFractionDigits;

numberFormat.maximumSignificantDigits =
referenceNumberFormat.maximumSignificantDigits;
numberFormat.minimumSignificantDigits =
referenceNumberFormat.minimumSignificantDigits;
} on Exception catch (_) {
numberFormat = new NumberFormat.currency(
locale: "en_US", symbol: "", decimalDigits: decimalDigits);
}

bool mustRemoveGrouping = (userDefinedGroupSeparator != null &&
userDefinedGroupSeparator.isEmpty) ||
turnOffGrouping;

if (mustRemoveGrouping) {
numberFormat.turnOffGrouping();
}

return numberFormat;
}

String getCurrencyValueString(double? value,
{turnOffGrouping = false, locale}) {
String getCurrencyValueString(double? value, {turnOffGrouping = false}) {
if (value == null) return "";
NumberFormat numberFormat = getNumberFormatWithCustomizations(
turnOffGrouping: turnOffGrouping, locale: locale);
NumberFormat numberFormat;
if (turnOffGrouping) {
numberFormat = ServiceConfig.currencyNumberFormatWithoutGrouping!;
} else {
numberFormat = ServiceConfig.currencyNumberFormat!;
}
return numberFormat.format(value);
}

double? tryParseCurrencyString(String toParse) {
try {
NumberFormat numberFormat = getNumberFormatWithCustomizations();
NumberFormat numberFormat = ServiceConfig.currencyNumberFormat!;
double r = numberFormat.parse(toParse).toDouble();
return r;
} catch (e) {
Expand Down
10 changes: 6 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class OinkoinAppState extends State<OinkoinApp> {
}

setCurrencyLocale(attemptedLocale);

return attemptedLocale;
},
supportedLocales: [
Expand Down Expand Up @@ -153,16 +154,17 @@ class OinkoinAppState extends State<OinkoinApp> {
}
checkForSettingInconsistency(toSet);
ServiceConfig.currencyLocale = toSet;
ServiceConfig.currencyNumberFormat = getNumberFormatWithCustomizations();
ServiceConfig.currencyNumberFormat = getNumberFormatWithCustomizations(locale: toSet);
ServiceConfig.currencyNumberFormatWithoutGrouping = getNumberFormatWithCustomizations(locale: toSet, turnOffGrouping: true);
}

void checkForSettingInconsistency(Locale toSet) {
// Custom Group Separator Inconsistency
bool userDefinedGroupingSeparator =
ServiceConfig.sharedPreferences!.containsKey("groupSeparator");
if (userDefinedGroupingSeparator) {
String groupingSeparatorByTheUser = getUserDefinedGroupingSeparator()!;
if (groupingSeparatorByTheUser == getLocaleDecimalSeparator()) {
String groupingSeparatorByTheUser = getGroupingSeparator();
if (groupingSeparatorByTheUser == getDecimalSeparator()) {
// It may happen when a custom groupSeparator is set
// then the app language is changed
// in this case, reset the user preferences
Expand All @@ -174,7 +176,7 @@ class OinkoinAppState extends State<OinkoinApp> {
bool userDefinedOverwriteDotWithComma = ServiceConfig.sharedPreferences!
.containsKey("overwriteDotValueWithComma");
if (userDefinedOverwriteDotWithComma &&
getLocaleDecimalSeparator() != ",") {
getDecimalSeparator() != ",") {
// overwriteDotValueWithComma possible just when decimal separator is ,
ServiceConfig.sharedPreferences?.remove("overwriteDotValueWithComma");
}
Expand Down
2 changes: 1 addition & 1 deletion lib/records/edit-record-page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class EditRecordPageState extends State<EditRecordPage> {

String? tryParseMathExpr(String text) {
var groupingSeparator = getGroupingSeparator();
var decimalSeparator = getLocaleDecimalSeparator();
var decimalSeparator = getDecimalSeparator();
if (isMathExpression(text)) {
try {
text = text.replaceAll(groupingSeparator, "");
Expand Down
1 change: 1 addition & 0 deletions lib/services/service-config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ class ServiceConfig {
static String? version; // set in main.dart
static Locale? currencyLocale; // set in main.dart
static NumberFormat? currencyNumberFormat; // set in main.dart
static NumberFormat? currencyNumberFormatWithoutGrouping; // set in main.dart
}
1 change: 1 addition & 0 deletions lib/settings/dropdown-customization-item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class DropdownCustomizationItemState<T>
setState(() {
// Make the currencyFormat invalid
ServiceConfig.currencyNumberFormat = null;
ServiceConfig.currencyNumberFormatWithoutGrouping = null;
selectedDropdownKey = value!;
});
},
Expand Down

0 comments on commit e7d7116

Please sign in to comment.