26 formatter( QChar thousands,
bool showThousands, QChar decimal )
46 return QStringLiteral(
"basic" );
51 return QObject::tr(
"Number" );
61 const QChar decimal = mDecimalSeparator.isNull() ? context.
decimalSeparator() : mDecimalSeparator;
62 std::basic_stringstream<wchar_t> os;
63 os.imbue( std::locale( os.getloc(),
new formatter( mThousandsSeparator.isNull() ? context.
thousandsSeparator() : mThousandsSeparator,
64 mShowThousandsSeparator,
69 switch ( mRoundingType )
72 os << std::fixed << std::setprecision( mNumberDecimalPlaces );
80 os << std::fixed << std::setprecision( mNumberDecimalPlaces - 1 ) << value;
85 const int d = std::floor( std::log10( value < 0 ? -value : value ) ) + 1;
86 const double order = std::pow( 10.0, mNumberDecimalPlaces - d );
87 os << std::fixed << std::setprecision( std::max( mNumberDecimalPlaces - d, 0 ) ) << std::round( value * order ) / order;
95 os << std::scientific << std::setprecision( mNumberDecimalPlaces );
99 QString res = QString::fromStdWString( os.str() );
101 if ( mShowPlusSign && value > 0 )
104 if ( !mShowTrailingZeros && res.contains( decimal ) )
106 int trimPoint = res.length() - 1;
110 while ( res.at( trimPoint ).toUpper() != context.
exponential().toUpper() )
116 while ( res.at( trimPoint ) == context.
zeroDigit() )
119 if ( res.at( trimPoint ) == decimal )
122 const QString original = res;
123 res.truncate( trimPoint + 1 );
125 res += original.mid( ePoint );
138 std::unique_ptr< QgsBasicNumericFormat > res = std::make_unique< QgsBasicNumericFormat >();
140 return res.release();
146 res.insert( QStringLiteral(
"decimals" ), mNumberDecimalPlaces );
147 res.insert( QStringLiteral(
"show_thousand_separator" ), mShowThousandsSeparator );
148 res.insert( QStringLiteral(
"show_plus" ), mShowPlusSign );
149 res.insert( QStringLiteral(
"show_trailing_zeros" ), mShowTrailingZeros );
150 res.insert( QStringLiteral(
"rounding_type" ),
static_cast< int >( mRoundingType ) );
151 res.insert( QStringLiteral(
"thousand_separator" ), mThousandsSeparator );
152 res.insert( QStringLiteral(
"decimal_separator" ), mDecimalSeparator );
158 mNumberDecimalPlaces =
configuration.value( QStringLiteral(
"decimals" ), 6 ).toInt();
159 mShowThousandsSeparator =
configuration.value( QStringLiteral(
"show_thousand_separator" ),
true ).toBool();
160 mShowPlusSign =
configuration.value( QStringLiteral(
"show_plus" ),
false ).toBool();
161 mShowTrailingZeros =
configuration.value( QStringLiteral(
"show_trailing_zeros" ),
false ).toBool();
163 mThousandsSeparator =
configuration.value( QStringLiteral(
"thousand_separator" ), QChar() ).toChar();
164 mDecimalSeparator =
configuration.value( QStringLiteral(
"decimal_separator" ), QChar() ).toChar();
169 return mNumberDecimalPlaces;
179 return mShowThousandsSeparator;
189 return mShowPlusSign;
199 return mShowTrailingZeros;
209 return mRoundingType;
214 mRoundingType = type;
219 return mThousandsSeparator;
224 mThousandsSeparator = character;
229 return mDecimalSeparator;
234 mDecimalSeparator = character;
A context for numeric formats.
QChar thousandsSeparator() const
Returns the thousands separator character.
QChar zeroDigit() const
Returns the zero digit character.
QChar exponential() const
Returns the exponential character.
QChar decimalSeparator() const
Returns the decimal separator character.
QChar positiveSign() const
Returns the positive sign character.
The class is used as a container of context for various read/write operations on other objects.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)