28 formatter( QChar thousands,
bool showThousands, QChar decimal )
49 return QStringLiteral(
"basic" );
54 return QObject::tr(
"Number" );
64 const QChar decimal = mDecimalSeparator.isNull() ? context.
decimalSeparator() : mDecimalSeparator;
65 std::basic_stringstream<wchar_t> os;
67 mShowThousandsSeparator,
72 switch ( mRoundingType )
75 os << std::fixed << std::setprecision( mNumberDecimalPlaces );
87 os << std::fixed << std::setprecision( mNumberDecimalPlaces - 1 ) << 0.0;
92 const int d = std::floor( std::log10( value < 0 ? -value : value ) ) + 1;
93 const double order = std::pow( 10.0, mNumberDecimalPlaces - d );
94 os << std::fixed << std::setprecision( std::max( mNumberDecimalPlaces - d, 0 ) ) << std::round( value * order ) / order;
102 os << std::scientific << std::setprecision( mNumberDecimalPlaces );
109 QString res = QString::fromStdWString( os.str() );
111 if ( mShowPlusSign && value > 0 )
114 if ( !mShowTrailingZeros && res.contains( decimal ) )
116 int trimPoint = res.length() - 1;
120 while ( res.at( trimPoint ).toUpper() != context.
exponential().toUpper() )
126 while ( res.at( trimPoint ) == context.
zeroDigit() )
129 if ( res.at( trimPoint ) == decimal )
132 const QString original = res;
133 res.truncate( trimPoint + 1 );
135 res += original.mid( ePoint );
148 std::unique_ptr< QgsBasicNumericFormat > res = std::make_unique< QgsBasicNumericFormat >();
150 return res.release();
156 res.insert( QStringLiteral(
"decimals" ), mNumberDecimalPlaces );
157 res.insert( QStringLiteral(
"show_thousand_separator" ), mShowThousandsSeparator );
158 res.insert( QStringLiteral(
"show_plus" ), mShowPlusSign );
159 res.insert( QStringLiteral(
"show_trailing_zeros" ), mShowTrailingZeros );
160 res.insert( QStringLiteral(
"rounding_type" ),
static_cast< int >( mRoundingType ) );
161 res.insert( QStringLiteral(
"thousand_separator" ), mThousandsSeparator.isNull() ? QVariant() : QVariant::fromValue( mThousandsSeparator ) );
162 res.insert( QStringLiteral(
"decimal_separator" ), mDecimalSeparator.isNull() ? QVariant() : QVariant::fromValue( mDecimalSeparator ) );
168 mNumberDecimalPlaces =
configuration.value( QStringLiteral(
"decimals" ), 6 ).toInt();
169 mShowThousandsSeparator =
configuration.value( QStringLiteral(
"show_thousand_separator" ),
true ).toBool();
170 mShowPlusSign =
configuration.value( QStringLiteral(
"show_plus" ),
false ).toBool();
171 mShowTrailingZeros =
configuration.value( QStringLiteral(
"show_trailing_zeros" ),
false ).toBool();
173 mThousandsSeparator =
configuration.value( QStringLiteral(
"thousand_separator" ), QChar() ).toChar();
174 mDecimalSeparator =
configuration.value( QStringLiteral(
"decimal_separator" ), QChar() ).toChar();
179 return mNumberDecimalPlaces;
189 return mShowThousandsSeparator;
199 return mShowPlusSign;
209 return mShowTrailingZeros;
219 return mRoundingType;
224 mRoundingType = type;
229 return mThousandsSeparator;
234 mThousandsSeparator = character;
239 return mDecimalSeparator;
244 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)