30 formatter( QChar thousands,
bool showThousands, QChar decimal )
51 return QStringLiteral(
"basic" );
56 return QObject::tr(
"Number" );
66 const QChar decimal = mDecimalSeparator.isNull() ? context.
decimalSeparator() : mDecimalSeparator;
67 std::basic_stringstream<wchar_t> os;
69 mShowThousandsSeparator,
74 switch ( mRoundingType )
77 os << std::fixed << std::setprecision( mNumberDecimalPlaces );
89 os << std::fixed << std::setprecision( mNumberDecimalPlaces - 1 ) << 0.0;
94 const int d = std::floor( std::log10( value < 0 ? -value : value ) ) + 1;
95 const double order = std::pow( 10.0, mNumberDecimalPlaces - d );
96 os << std::fixed << std::setprecision( std::max( mNumberDecimalPlaces - d, 0 ) ) << std::round( value * order ) / order;
104 os << std::scientific << std::setprecision( mNumberDecimalPlaces );
111 QString res = QString::fromStdWString( os.str() );
113 if ( mShowPlusSign && value > 0 )
116 if ( !mShowTrailingZeros && res.contains( decimal ) )
118 int trimPoint = res.length() - 1;
122 while ( res.at( trimPoint ).toUpper() != context.
exponential().toUpper() )
128 while ( res.at( trimPoint ) == context.
zeroDigit() )
131 if ( res.at( trimPoint ) == decimal )
134 const QString original = res;
135 res.truncate( trimPoint + 1 );
137 res += original.mid( ePoint );
150 auto res = std::make_unique< QgsBasicNumericFormat >();
152 return res.release();
158 res.insert( QStringLiteral(
"decimals" ), mNumberDecimalPlaces );
159 res.insert( QStringLiteral(
"show_thousand_separator" ), mShowThousandsSeparator );
160 res.insert( QStringLiteral(
"show_plus" ), mShowPlusSign );
161 res.insert( QStringLiteral(
"show_trailing_zeros" ), mShowTrailingZeros );
162 res.insert( QStringLiteral(
"rounding_type" ),
static_cast< int >( mRoundingType ) );
163 res.insert( QStringLiteral(
"thousand_separator" ), mThousandsSeparator.isNull() ? QVariant() : QVariant::fromValue( mThousandsSeparator ) );
164 res.insert( QStringLiteral(
"decimal_separator" ), mDecimalSeparator.isNull() ? QVariant() : QVariant::fromValue( mDecimalSeparator ) );
170 mNumberDecimalPlaces =
configuration.value( QStringLiteral(
"decimals" ), 6 ).toInt();
171 mShowThousandsSeparator =
configuration.value( QStringLiteral(
"show_thousand_separator" ),
true ).toBool();
172 mShowPlusSign =
configuration.value( QStringLiteral(
"show_plus" ),
false ).toBool();
173 mShowTrailingZeros =
configuration.value( QStringLiteral(
"show_trailing_zeros" ),
false ).toBool();
175 mThousandsSeparator =
configuration.value( QStringLiteral(
"thousand_separator" ), QChar() ).toChar();
176 mDecimalSeparator =
configuration.value( QStringLiteral(
"decimal_separator" ), QChar() ).toChar();
181 return mNumberDecimalPlaces;
191 return mShowThousandsSeparator;
201 return mShowPlusSign;
211 return mShowTrailingZeros;
221 return mRoundingType;
226 mRoundingType = type;
231 return mThousandsSeparator;
236 mThousandsSeparator = character;
241 return mDecimalSeparator;
246 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).