From 5299d8291de3b0ab2522ede6b1b7ee1e1f81f864 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Sat, 15 Aug 2020 13:46:16 -0700 Subject: [PATCH] core/localized-parsing: correctly handle group and decimal separator We are usually showing pressures with localized group separator. And we made a total mess out of things when then re-parsing those values. This caused us to ignore start and end pressures in Subsurface-mobile when those were entered in psi and included a group separator: 2,900psi was turned into 2.900psi which we then rounded to 0 mbar. This fixes the problem by asking Qt to do the right thing instead of doing stupid separator magic. Signed-off-by: Dirk Hohndel --- core/qthelper.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/core/qthelper.cpp b/core/qthelper.cpp index 656640755..0de3e6002 100644 --- a/core/qthelper.cpp +++ b/core/qthelper.cpp @@ -829,10 +829,16 @@ int parsePressureToMbar(const QString &text) { int mbar; QString numOnly = text; - numOnly.replace(",", ".").remove(QRegExp("[^0-9.]")); + // different locales use different symbols as group separator or decimal separator + // (I think it's usually '.' and ',' - but maybe there are others?) + // let's use Qt's help to get the parsing right + QString validNumberCharacters("0-9"); + validNumberCharacters += loc.decimalPoint(); + validNumberCharacters += loc.groupSeparator(); + numOnly.remove(QRegExp(QString("[^%1]").arg(validNumberCharacters))); if (numOnly.isEmpty()) return 0; - double number = numOnly.toDouble(); + double number = loc.toDouble(numOnly); if (text.contains(gettextFromC::tr("bar"), Qt::CaseInsensitive)) { mbar = lrint(number * 1000); } else if (text.contains(gettextFromC::tr("psi"), Qt::CaseInsensitive)) {