if ( qAbs(theValue) < prec )
return "0";
- QString aRes = QLocale().toString( theValue, 'g', qAbs( thePrecision ) );
+ QString aRes = QLocale().toString( theValue, thePrecision >= 0 ? 'f' : 'g', qAbs( thePrecision ) );
if ( prec > 0 ) {
int p = 0;
while ( p < thePrecision ) {
- QString aRes = QLocale().toString( theValue, 'g', qAbs( p++ ) );
+ QString aRes = QLocale().toString( theValue, thePrecision >= 0 ? 'f' : 'g', qAbs( p++ ) );
double v = aRes.toDouble();
double err = qAbs( theValue - v );
if ( err > 0 && err <= prec )
}
// remove trailing zeroes
- QString delim( QLocale().decimalPoint() );
- int idx = aRes.lastIndexOf( delim );
- if ( idx == -1 )
- return aRes;
+ QRegExp expre( QString( "(%1|%2)[+-]?[0-9]+$" ).arg( QLocale().exponential().toLower(),
+ QLocale().exponential().toUpper() ) );
- QString iPart = aRes.left( idx );
- QString fPart = aRes.mid( idx + 1 );
-
- while ( !fPart.isEmpty() && fPart.at( fPart.length() - 1 ) == '0' )
- fPart.remove( fPart.length() - 1, 1 );
+ int idx = aRes.indexOf( expre );
+ QString aResExp = "";
+ if ( idx >= 0 ) {
+ aResExp = aRes.mid( idx );
+ aRes = aRes.left( idx );
+ }
- aRes = iPart;
- if ( !fPart.isEmpty() )
- aRes += delim + fPart;
+ if ( aRes.contains( QLocale().decimalPoint() ) )
+ aRes.remove( QRegExp( QString( "(\\%1|0)0*$" ).arg( QLocale().decimalPoint() ) ) );
- return aRes;
+ return aRes == "-0" ? QString( "0" ) : aRes + aResExp;
}