Skip to content
Permalink
Browse files

Wide characters should be supported for commas and decimal point symbols

  • Loading branch information
nyalldawson committed Jan 8, 2020
1 parent fd970b9 commit b9b4f884c4005d7db2000b0f7760e18b700dc41f
Showing with 18 additions and 18 deletions.
  1. +10 −10 src/core/numericformats/qgsbasicnumericformat.cpp
  2. +8 −8 tests/src/python/test_qgsnumericformat.py
@@ -21,19 +21,19 @@
#include <locale>
#include <iomanip>

struct formatter : std::numpunct<char>
struct formatter : std::numpunct<wchar_t>
{
formatter( QChar thousands, bool showThousands, QChar decimal )
: mThousands( thousands.toLatin1() )
, mDecimal( decimal.toLatin1() )
: mThousands( thousands.unicode() )
, mDecimal( decimal.unicode() )
, mShowThousands( showThousands )
{}
char do_decimal_point() const override {return mDecimal;}
char do_thousands_sep() const override {return mThousands;}
string_type do_grouping() const override { return mShowThousands ? "\3" : "\0"; }
wchar_t do_decimal_point() const override { return mDecimal; }
wchar_t do_thousands_sep() const override { return mThousands; }
std::string do_grouping() const override { return mShowThousands ? "\3" : "\0"; }

char mThousands;
char mDecimal;
wchar_t mThousands;
wchar_t mDecimal;
bool mShowThousands = true;
};

@@ -58,7 +58,7 @@ int QgsBasicNumericFormat::sortKey()

QString QgsBasicNumericFormat::formatDouble( double value, const QgsNumericFormatContext &context ) const
{
std::ostringstream os;
std::basic_stringstream<wchar_t> os;
os.imbue( std::locale( os.getloc(), new formatter( context.thousandsSeparator(), mShowThousandsSeparator, context.decimalSeparator() ) ) );

if ( !mUseScientific )
@@ -67,7 +67,7 @@ QString QgsBasicNumericFormat::formatDouble( double value, const QgsNumericForma
os << std::scientific << std::setprecision( mNumberDecimalPlaces );

os << value;
QString res = QString::fromStdString( os.str() );
QString res = QString::fromStdWString( os.str() );

if ( mShowPlusSign && value > 0 )
res.prepend( context.positiveSign() );
@@ -81,16 +81,16 @@ def testBasicFormat(self):
self.assertEqual(f.formatDouble(-5, context), '-5')
self.assertEqual(f.formatDouble(-5.5, context), '-5.5')
self.assertEqual(f.formatDouble(-55555555.5, context), '-55,555,555.5')
context.setDecimalSeparator('x')
context.setDecimalSeparator('')
self.assertEqual(f.formatDouble(0, context), '0')
self.assertEqual(f.formatDouble(-5.5, context), '-5x5')
self.assertEqual(f.formatDouble(-55555555.5, context), '-55,555,555x5')
context.setThousandsSeparator('y')
self.assertEqual(f.formatDouble(-5.5, context), '-5x5')
self.assertEqual(f.formatDouble(-55555555.5, context), '-55y555y555x5')
self.assertEqual(f.formatDouble(-5.5, context), '-5☕5')
self.assertEqual(f.formatDouble(-55555555.5, context), '-55,555,555☕5')
context.setThousandsSeparator('')
self.assertEqual(f.formatDouble(-5.5, context), '-5☕5')
self.assertEqual(f.formatDouble(-55555555.5, context), '-55⚡555⚡555☕5')
f.setShowThousandsSeparator(False)
self.assertEqual(f.formatDouble(-5.5, context), '-5x5')
self.assertEqual(f.formatDouble(-55555555.5, context), '-55555555x5')
self.assertEqual(f.formatDouble(-5.5, context), '-5☕5')
self.assertEqual(f.formatDouble(-55555555.5, context), '-55555555☕5')
context.setDecimalSeparator('.')
f.setNumberDecimalPlaces(0)
self.assertEqual(f.formatDouble(0, context), '0')

0 comments on commit b9b4f88

Please sign in to comment.
You can’t perform that action at this time.