Skip to content
Permalink
Browse files

Skip custom handling of decimal point if it's a dot

  • Loading branch information
elpaso committed Jun 9, 2018
1 parent f853c8d commit a85f67ee9152c835d117e558b8ee396c10ed0ade
Showing with 81 additions and 21 deletions.
  1. +40 −19 src/core/qgsfield.cpp
  2. +41 −2 tests/src/core/testqgsfield.cpp
@@ -209,31 +209,48 @@ QString QgsField::displayString( const QVariant &v ) const
return QgsApplication::nullRepresentation();
}

// Special treatment for numeric types if group separator is set or decimalPoint is not a dot
if ( d->type == QVariant::Double )
{
if ( d->precision > 0 )
// Locales with decimal point != '.' or that require group separator: use QLocale
if ( QLocale().decimalPoint() != '.' ||
!( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
{
return QLocale().toString( v.toDouble(), 'f', d->precision );
}
else
{
// Precision is not set, let's guess it from the
// standard conversion to string
QString s( v.toString() );
int dotPosition( s.indexOf( '.' ) );
int precision;
if ( dotPosition < 0 )
if ( d->precision > 0 )
{
precision = 0;
return QLocale().toString( v.toDouble(), 'f', d->precision );
}
else
{
precision = s.length() - dotPosition - 1;
// Precision is not set, let's guess it from the
// standard conversion to string
QString s( v.toString() );
int dotPosition( s.indexOf( '.' ) );
int precision;
if ( dotPosition < 0 )
{
precision = 0;
}
else
{
precision = s.length() - dotPosition - 1;
}
return QLocale().toString( v.toDouble(), 'f', precision );
}
return QLocale().toString( v.toDouble(), 'f', precision );
}
// Default for doubles with precision
else if ( d->type == QVariant::Double && d->precision > 0 )
{
return QString::number( v.toDouble(), 'f', d->precision );
}
}

// Other numeric types out of doubles
else if ( isNumeric() &&
! QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator )
{
return QLocale().toString( v.toLongLong() );
}
// Fallback if special rules do not apply
return v.toString();
}

@@ -281,11 +298,15 @@ bool QgsField::convertCompatible( QVariant &v ) const
return true;
}

// Give it a chance to convert to double since we accept both comma and dot as decimal point
QVariant tmp( v );
if ( d->type == QVariant::Double && !tmp.convert( d->type ) )
// Give it a chance to convert to double since for not '.' locales
// we accept both comma and dot as decimal point
if ( d->type == QVariant::Double && QLocale().decimalPoint() != '.' )
{
v = v.toString().replace( ',', '.' );
QVariant tmp( v );
if ( d->type == QVariant::Double && !tmp.convert( d->type ) )
{
v = v.toString().replace( ',', '.' );
}
}

if ( !v.convert( d->type ) )
@@ -316,12 +316,12 @@ void TestQgsField::displayString()
//test int value in int type
QgsField intField2( QStringLiteral( "int" ), QVariant::Int, QStringLiteral( "int" ) );
QCOMPARE( intField2.displayString( 5 ), QString( "5" ) );
QCOMPARE( intField2.displayString( 599999898999LL ), QString( "599999898999" ) );
QCOMPARE( intField2.displayString( 599999898999LL ), QString( "599,999,898,999" ) );

//test long type
QgsField longField( QStringLiteral( "long" ), QVariant::LongLong, QStringLiteral( "longlong" ) );
QCOMPARE( longField.displayString( 5 ), QString( "5" ) );
QCOMPARE( longField.displayString( 599999898999LL ), QString( "599999898999" ) );
QCOMPARE( longField.displayString( 599999898999LL ), QString( "599,999,898,999" ) );

//test NULL int
QVariant nullInt = QVariant( QVariant::Int );
@@ -333,6 +333,8 @@ void TestQgsField::displayString()
QgsField doubleFieldNoPrec( QStringLiteral( "double" ), QVariant::Double, QStringLiteral( "double" ), 10 );
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005 ), QString( "5.005005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005005 ), QString( "5.005005005" ) );
QCOMPARE( QLocale().decimalPoint(), '.' );
QCOMPARE( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator, QLocale::NumberOption::DefaultNumberOptions );
QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599,999,898,999" ) );

//test NULL double
@@ -345,6 +347,43 @@ void TestQgsField::displayString()
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005 ), QString( "5,005005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005005 ), QString( "5,005005005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599.999.898.999" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5999.123456 ), QString( "5.999,123456" ) );

//test value with custom German locale (OmitGroupSeparator)
QLocale customGerman( QLocale::German );
customGerman.setNumberOptions( QLocale::NumberOption::OmitGroupSeparator );
QLocale::setDefault( customGerman );
QCOMPARE( doubleField.displayString( 5.005005 ), QString( "5,005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005 ), QString( "5,005005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005005 ), QString( "5,005005005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599999898999" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5999.123456 ), QString( "5999,123456" ) );

//test int value in int type with custom German locale (OmitGroupSeparator)
QCOMPARE( intField2.displayString( 5 ), QString( "5" ) );
QCOMPARE( intField2.displayString( 599999898999LL ), QString( "599999898999" ) );

//test long type with custom German locale (OmitGroupSeparator)
QCOMPARE( longField.displayString( 5 ), QString( "5" ) );
QCOMPARE( longField.displayString( 599999898999LL ), QString( "599999898999" ) );

//test value with custom english locale (OmitGroupSeparator)
QLocale customEnglish( QLocale::English );
customEnglish.setNumberOptions( QLocale::NumberOption::OmitGroupSeparator );
QLocale::setDefault( customEnglish );
QCOMPARE( doubleField.displayString( 5.005005 ), QString( "5.005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005 ), QString( "5.005005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005005 ), QString( "5.005005005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599999898999" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5999.123456 ), QString( "5999.123456" ) );

//test int value in int type with custom english locale (OmitGroupSeparator)
QCOMPARE( intField2.displayString( 5 ), QString( "5" ) );
QCOMPARE( intField2.displayString( 599999898999LL ), QString( "599999898999" ) );

//test long type with custom english locale (OmitGroupSeparator)
QCOMPARE( longField.displayString( 5 ), QString( "5" ) );
QCOMPARE( longField.displayString( 599999898999LL ), QString( "599999898999" ) );

}

0 comments on commit a85f67e

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