28using namespace Qt::StringLiterals;
34 formatter( QChar thousands,
bool showThousands, QChar decimal )
60 return QObject::tr(
"Number" );
70 const QChar decimal = mDecimalSeparator.isNull() ? context.
decimalSeparator() : mDecimalSeparator;
71 std::basic_stringstream<wchar_t> os;
73 mShowThousandsSeparator,
78 switch ( mRoundingType )
81 os << std::fixed << std::setprecision( mNumberDecimalPlaces );
93 os << std::fixed << std::setprecision( mNumberDecimalPlaces - 1 ) << 0.0;
98 const int d = std::floor( std::log10( value < 0 ? -value : value ) ) + 1;
99 const double order = std::pow( 10.0, mNumberDecimalPlaces - d );
100 os << std::fixed << std::setprecision( std::max( mNumberDecimalPlaces - d, 0 ) ) << std::round( value * order ) / order;
108 os << std::scientific << std::setprecision( mNumberDecimalPlaces );
115 QString res = QString::fromStdWString( os.str() );
117 if ( mShowPlusSign && value > 0 )
120 if ( !mShowTrailingZeros && res.contains( decimal ) )
122 int trimPoint = res.length() - 1;
126 while ( res.at( trimPoint ).toUpper() != context.
exponential().toUpper() )
132 while ( res.at( trimPoint ) == context.
zeroDigit() )
135 if ( res.at( trimPoint ) == decimal )
138 const QString original = res;
139 res.truncate( trimPoint + 1 );
141 res += original.mid( ePoint );
154 auto res = std::make_unique< QgsBasicNumericFormat >();
156 return res.release();
162 res.insert( u
"decimals"_s, mNumberDecimalPlaces );
163 res.insert( u
"show_thousand_separator"_s, mShowThousandsSeparator );
164 res.insert( u
"show_plus"_s, mShowPlusSign );
165 res.insert( u
"show_trailing_zeros"_s, mShowTrailingZeros );
166 res.insert( u
"rounding_type"_s,
static_cast< int >( mRoundingType ) );
167 res.insert( u
"thousand_separator"_s, mThousandsSeparator.isNull() ? QVariant() : QVariant::fromValue( mThousandsSeparator ) );
168 res.insert( u
"decimal_separator"_s, mDecimalSeparator.isNull() ? QVariant() : QVariant::fromValue( mDecimalSeparator ) );
174 mNumberDecimalPlaces =
configuration.value( u
"decimals"_s, 6 ).toInt();
175 mShowThousandsSeparator =
configuration.value( u
"show_thousand_separator"_s,
true ).toBool();
176 mShowPlusSign =
configuration.value( u
"show_plus"_s,
false ).toBool();
177 mShowTrailingZeros =
configuration.value( u
"show_trailing_zeros"_s,
false ).toBool();
179 mThousandsSeparator =
configuration.value( u
"thousand_separator"_s, QChar() ).toChar();
180 mDecimalSeparator =
configuration.value( u
"decimal_separator"_s, QChar() ).toChar();
185 return mNumberDecimalPlaces;
195 return mShowThousandsSeparator;
205 return mShowPlusSign;
215 return mShowTrailingZeros;
225 return mRoundingType;
230 mRoundingType = type;
235 return mThousandsSeparator;
240 mThousandsSeparator = character;
245 return mDecimalSeparator;
250 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).