26 formatter( QChar thousands,
bool showThousands, QChar decimal )
46 return QStringLiteral(
"basic" );
51 return QObject::tr(
"Number" );
61 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,
67 if ( !mUseScientific )
69 switch ( mRoundingType )
72 os << std::fixed << std::setprecision( mNumberDecimalPlaces );
76 case SignificantFigures:
80 os << std::fixed << std::setprecision( mNumberDecimalPlaces - 1 ) << value;
85 const int d = std::floor( std::log10( value < 0 ? -value : value ) ) + 1;
86 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;
108 if ( mUseScientific )
110 while ( res.at( trimPoint ).toUpper() != context.
exponential().toUpper() )
116 while ( res.at( trimPoint ) == context.
zeroDigit() )
119 if ( res.at( trimPoint ) == decimal )
122 QString original = res;
123 res.truncate( trimPoint + 1 );
124 if ( mUseScientific )
125 res += original.mid( ePoint );
138 std::unique_ptr< QgsBasicNumericFormat > res = qgis::make_unique< QgsBasicNumericFormat >();
139 res->setConfiguration( configuration, context );
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();
162 mRoundingType =
static_cast< RoundingType >( configuration.value( QStringLiteral(
"rounding_type" ), static_cast< int >( DecimalPlaces ) ).toInt() );
163 mThousandsSeparator = configuration.value( QStringLiteral(
"thousand_separator" ), QChar() ).toChar();
164 mDecimalSeparator = configuration.value( QStringLiteral(
"decimal_separator" ), QChar() ).toChar();
169 return mNumberDecimalPlaces;
174 mNumberDecimalPlaces = numberDecimalPlaces;
179 return mShowThousandsSeparator;
184 mShowThousandsSeparator = showThousandsSeparator;
189 return mShowPlusSign;
194 mShowPlusSign = showPlusSign;
199 return mShowTrailingZeros;
204 mShowTrailingZeros = showTrailingZeros;
209 return mRoundingType;
214 mRoundingType = type;
219 return mThousandsSeparator;
224 mThousandsSeparator = character;
229 return mDecimalSeparator;
234 mDecimalSeparator = character;
QChar thousandsSeparator() const
Returns the thousands separator 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)
QChar positiveSign() const
Returns the positive sign character.
QChar exponential() const
Returns the exponential character.
QChar zeroDigit() const
Returns the zero digit character.
QChar decimalSeparator() const
Returns the decimal separator character.
A context for numeric formats.