28using namespace Qt::StringLiterals;
34 formatter( QChar thousands,
bool showThousands, QChar decimal )
59 return QObject::tr(
"Number" );
69 const QChar decimal = mDecimalSeparator.isNull() ? context.
decimalSeparator() : mDecimalSeparator;
70 std::basic_stringstream<wchar_t> os;
75 switch ( mRoundingType )
78 os << std::fixed << std::setprecision( mNumberDecimalPlaces );
90 os << std::fixed << std::setprecision( mNumberDecimalPlaces - 1 ) << 0.0;
95 const int d = std::floor( std::log10( value < 0 ? -value : value ) ) + 1;
96 const double order = std::pow( 10.0, mNumberDecimalPlaces - d );
97 os << std::fixed << std::setprecision( std::max( mNumberDecimalPlaces - d, 0 ) ) << std::round( value * order ) / order;
105 os << std::scientific << std::setprecision( mNumberDecimalPlaces );
112 QString res = QString::fromStdWString( os.str() );
114 if ( mShowPlusSign && value > 0 )
117 if ( !mShowTrailingZeros && res.contains( decimal ) )
119 int trimPoint = res.length() - 1;
123 while ( res.at( trimPoint ).toUpper() != context.
exponential().toUpper() )
129 while ( res.at( trimPoint ) == context.
zeroDigit() )
132 if ( res.at( trimPoint ) == decimal )
135 const QString original = res;
136 res.truncate( trimPoint + 1 );
138 res += original.mid( ePoint );
151 auto res = std::make_unique< QgsBasicNumericFormat >();
153 return res.release();
159 res.insert( u
"decimals"_s, mNumberDecimalPlaces );
160 res.insert( u
"show_thousand_separator"_s, mShowThousandsSeparator );
161 res.insert( u
"show_plus"_s, mShowPlusSign );
162 res.insert( u
"show_trailing_zeros"_s, mShowTrailingZeros );
163 res.insert( u
"rounding_type"_s,
static_cast< int >( mRoundingType ) );
164 res.insert( u
"thousand_separator"_s, mThousandsSeparator.isNull() ? QVariant() : QVariant::fromValue( mThousandsSeparator ) );
165 res.insert( u
"decimal_separator"_s, mDecimalSeparator.isNull() ? QVariant() : QVariant::fromValue( mDecimalSeparator ) );
171 mNumberDecimalPlaces =
configuration.value( u
"decimals"_s, 6 ).toInt();
172 mShowThousandsSeparator =
configuration.value( u
"show_thousand_separator"_s,
true ).toBool();
173 mShowPlusSign =
configuration.value( u
"show_plus"_s,
false ).toBool();
174 mShowTrailingZeros =
configuration.value( u
"show_trailing_zeros"_s,
false ).toBool();
176 mThousandsSeparator =
configuration.value( u
"thousand_separator"_s, QChar() ).toChar();
177 mDecimalSeparator =
configuration.value( u
"decimal_separator"_s, QChar() ).toChar();
182 return mNumberDecimalPlaces;
192 return mShowThousandsSeparator;
202 return mShowPlusSign;
212 return mShowTrailingZeros;
222 return mRoundingType;
227 mRoundingType = type;
232 return mThousandsSeparator;
237 mThousandsSeparator = character;
242 return mDecimalSeparator;
247 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.
A container for the context for various read/write operations on objects.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).