Skip to content
Permalink
Browse files

BUG: scientific notation for double number between 1 and -1, fix #33475

  • Loading branch information
speillet authored and mhugo committed Dec 19, 2019
1 parent f00cbb3 commit 5fe6bb89beb0c4895e03132e1fce3b5b2c070c80
Showing with 37 additions and 4 deletions.
  1. +21 −4 src/core/qgsfield.cpp
  2. +16 −0 tests/src/core/testqgsfield.cpp
@@ -244,21 +244,38 @@ QString QgsField::displayString( const QVariant &v ) const
QString s( v.toString() );
int dotPosition( s.indexOf( '.' ) );
int precision;
if ( dotPosition < 0 )
if ( dotPosition < 0 && s.indexOf( 'e' ) < 0 )
{
precision = 0;
return QLocale().toString( v.toDouble(), 'f', precision );
}
else
{
precision = s.length() - dotPosition - 1;
if ( dotPosition < 0 ) precision = 0;
else precision = s.length() - dotPosition - 1;

if ( -1 < v.toDouble() && v.toDouble() < 1 )
{
return QLocale().toString( v.toDouble(), 'g', precision );
}
else
{
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 );
if ( -1 < v.toDouble() && v.toDouble() < 1 )
{
return QString::number( v.toDouble(), 'g', d->precision );
}
else
{
return QString::number( v.toDouble(), 'f', d->precision );
}
}
}
// Other numeric types than doubles
@@ -359,9 +359,13 @@ void TestQgsField::displayString()
//test double value
QgsField doubleField( QStringLiteral( "double" ), QVariant::Double, QStringLiteral( "double" ), 10, 3 );
QCOMPARE( doubleField.displayString( 5.005005 ), QString( "5.005" ) );
QCOMPARE( doubleField.displayString( 4.5e-09 ), QString( "4.5e-09" ) );
QCOMPARE( doubleField.displayString( 1e-04 ), QString( "0.0001" ) );
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( doubleFieldNoPrec.displayString( 4.5e-09 ), QString( "4.5e-09" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 1e-04 ), QString( "0.0001" ) );
QCOMPARE( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator, QLocale::NumberOption::DefaultNumberOptions );
QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599,999,898,999" ) );

@@ -372,8 +376,12 @@ void TestQgsField::displayString()
//test double value with German locale
QLocale::setDefault( QLocale::German );
QCOMPARE( doubleField.displayString( 5.005005 ), QString( "5,005" ) );
QCOMPARE( doubleField.displayString( 4.5e-09 ), QString( "4,5e-09" ) );
QCOMPARE( doubleField.displayString( 1e-04 ), QString( "0,0001" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005 ), QString( "5,005005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005005 ), QString( "5,005005005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 4.5e-09 ), QString( "4,5e-09" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 1e-04 ), QString( "0,0001" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599.999.898.999" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5999.123456 ), QString( "5.999,123456" ) );

@@ -382,8 +390,12 @@ void TestQgsField::displayString()
customGerman.setNumberOptions( QLocale::NumberOption::OmitGroupSeparator );
QLocale::setDefault( customGerman );
QCOMPARE( doubleField.displayString( 5.005005 ), QString( "5,005" ) );
QCOMPARE( doubleField.displayString( 4.5e-09 ), QString( "4,5e-09" ) );
QCOMPARE( doubleField.displayString( 1e-04 ), QString( "0,0001" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005 ), QString( "5,005005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005005 ), QString( "5,005005005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 4.5e-09 ), QString( "4,5e-09" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 1e-04 ), QString( "0,0001" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599999898999" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5999.123456 ), QString( "5999,123456" ) );

@@ -400,8 +412,12 @@ void TestQgsField::displayString()
customEnglish.setNumberOptions( QLocale::NumberOption::OmitGroupSeparator );
QLocale::setDefault( customEnglish );
QCOMPARE( doubleField.displayString( 5.005005 ), QString( "5.005" ) );
QCOMPARE( doubleField.displayString( 4.5e-09 ), QString( "4.5e-09" ) );
QCOMPARE( doubleField.displayString( 1e-04 ), QString( "0.0001" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005 ), QString( "5.005005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005005 ), QString( "5.005005005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 4.5e-09 ), QString( "4.5e-09" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 1e-04 ), QString( "0.0001" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599999898999" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5999.123456 ), QString( "5999.123456" ) );

0 comments on commit 5fe6bb8

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