Skip to content
Permalink
Browse files

Also respect other locale specific characters

  • Loading branch information
nyalldawson committed Jan 7, 2020
1 parent 545edcf commit 664ba234da7d915b4f928d5913c2a515ef185c7f
@@ -57,6 +57,76 @@ Returns the decimal separator character.
Returns the decimal separator character.

.. seealso:: :py:func:`setDecimalSeparator`
%End

QChar percent() const;
%Docstring
Returns the percent character.

.. seealso:: :py:func:`setPercent`
%End

void setPercent( const QChar &character );
%Docstring
Sets the percent ``character``.

.. seealso:: :py:func:`percent`
%End

QChar zeroDigit() const;
%Docstring
Returns the zero digit character.

.. seealso:: :py:func:`setZeroDigit`
%End

void setZeroDigit( const QChar &character );
%Docstring
Returns the zero digit ``character``.

.. seealso:: :py:func:`zeroDigit`
%End

QChar negativeSign() const;
%Docstring
Returns the negative sign character.

.. seealso:: :py:func:`setNegativeSign`
%End

void setNegativeSign( const QChar &character );
%Docstring
Sets the negative sign ``character``.

.. seealso:: :py:func:`negativeSign`
%End

QChar positiveSign() const;
%Docstring
Returns the positive sign character.

.. seealso:: :py:func:`setPositiveSign`
%End

void setPositiveSign( const QChar &character );
%Docstring
Sets the positive sign ``character``.

.. seealso:: :py:func:`positiveSign`
%End

QChar exponential() const;
%Docstring
Returns the exponential character.

.. seealso:: :py:func:`setExponential`
%End

void setExponential( const QChar &character );
%Docstring
Sets the exponential ``character``.

.. seealso:: :py:func:`exponential`
%End

};
@@ -70,21 +70,21 @@ QString QgsBasicNumericFormat::formatDouble( double value, const QgsNumericForma
QString res = QString::fromStdString( os.str() );

if ( mShowPlusSign && value > 0 )
res.prepend( '+' );
res.prepend( context.positiveSign() );

if ( !mShowTrailingZeros && res.contains( context.decimalSeparator() ) )
{
int trimPoint = res.length() - 1;
int ePoint = 0;
if ( mUseScientific )
{
while ( res.at( trimPoint ) != 'e' && res.at( trimPoint ) != 'E' )
while ( res.at( trimPoint ).toUpper() != context.exponential().toUpper() )
trimPoint--;
ePoint = trimPoint;
trimPoint--;
}

while ( res.at( trimPoint ) == '0' )
while ( res.at( trimPoint ) == context.zeroDigit() )
trimPoint--;

if ( res.at( trimPoint ) == context.decimalSeparator() )
@@ -24,6 +24,11 @@ QgsNumericFormatContext::QgsNumericFormatContext()
QLocale l;
mThousandsSep = l.groupSeparator();
mDecimalSep = l.decimalPoint();
mPercent = l.percent();
mZeroDigit = l.zeroDigit();
mNegativeSign = l.negativeSign();
mPositiveSign = l.positiveSign();
mExponential = l.exponential();
}

int QgsNumericFormat::sortKey()
@@ -83,9 +83,114 @@ class CORE_EXPORT QgsNumericFormatContext
mDecimalSep = separator;
}

/**
* Returns the percent character.
*
* \see setPercent()
*/
QChar percent() const
{
return mPercent;
}

/**
* Sets the percent \a character.
*
* \see percent()
*/
void setPercent( const QChar &character )
{
mPercent = character;
}

/**
* Returns the zero digit character.
*
* \see setZeroDigit()
*/
QChar zeroDigit() const
{
return mZeroDigit;
}

/**
* Returns the zero digit \a character.
*
* \see zeroDigit()
*/
void setZeroDigit( const QChar &character )
{
mZeroDigit = character;
}

/**
* Returns the negative sign character.
*
* \see setNegativeSign()
*/
QChar negativeSign() const
{
return mNegativeSign;
}

/**
* Sets the negative sign \a character.
*
* \see negativeSign()
*/
void setNegativeSign( const QChar &character )
{
mNegativeSign = character;
}

/**
* Returns the positive sign character.
*
* \see setPositiveSign()
*/
QChar positiveSign() const
{
return mPositiveSign;
}

/**
* Sets the positive sign \a character.
*
* \see positiveSign()
*/
void setPositiveSign( const QChar &character )
{
mPositiveSign = character;
}

/**
* Returns the exponential character.
*
* \see setExponential()
*/
QChar exponential() const
{
return mExponential;
}

/**
* Sets the exponential \a character.
*
* \see exponential()
*/
void setExponential( const QChar &character )
{
mExponential = character;
}

private:
QChar mThousandsSep;
QChar mDecimalSep;
QChar mPercent;
QChar mZeroDigit;
QChar mNegativeSign;
QChar mPositiveSign;
QChar mExponential;
};

#ifdef SIP_RUN
@@ -62,7 +62,7 @@ QString QgsPercentageNumericFormat::formatDouble( double value, const QgsNumeric
break;
}

return QgsBasicNumericFormat::formatDouble( value, context ) + '%';
return QgsBasicNumericFormat::formatDouble( value, context ) + context.percent();
}

QgsNumericFormat *QgsPercentageNumericFormat::clone() const
@@ -124,6 +124,10 @@ def testBasicFormat(self):
self.assertEqual(f.formatDouble(55555555.123456, context), '+55555555.123')
self.assertEqual(f.formatDouble(-5.5, context), '-5.500')
self.assertEqual(f.formatDouble(-55555555.5, context), '-55555555.500')
context.setPositiveSign('w')
self.assertEqual(f.formatDouble(5, context), 'w5.000')
self.assertEqual(f.formatDouble(-5, context), '-5.000')
self.assertEqual(f.formatDouble(5.5, context), 'w5.500')

f2 = f.clone()
self.assertIsInstance(f2, QgsBasicNumericFormat)
@@ -465,6 +469,11 @@ def testPercentageFormat(self):
self.assertEqual(f.formatDouble(5.5, context), '+550.000%')
self.assertEqual(f.formatDouble(-5.5, context), '-550.000%')

context.setPercent('p')
self.assertEqual(f.formatDouble(0, context), '0.000p')
self.assertEqual(f.formatDouble(5, context), '+500.000p')
self.assertEqual(f.formatDouble(-5, context), '-500.000p')

f2 = f.clone()
self.assertIsInstance(f2, QgsPercentageNumericFormat)

0 comments on commit 664ba23

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