BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdldfp_decimalformatconfig.h
Go to the documentation of this file.
1/// @file bdldfp_decimalformatconfig.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdldfp_decimalformatconfig.h -*-C++-*-
8#ifndef INCLUDED_BDLDFP_DECIMALFORMATCONFIG
9#define INCLUDED_BDLDFP_DECIMALFORMATCONFIG
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id$")
13
14/// @defgroup bdldfp_decimalformatconfig bdldfp_decimalformatconfig
15/// @brief Provide an attribute class to configure decimal formatting.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdldfp
19/// @{
20/// @addtogroup bdldfp_decimalformatconfig
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdldfp_decimalformatconfig-purpose"> Purpose</a>
25/// * <a href="#bdldfp_decimalformatconfig-classes"> Classes </a>
26/// * <a href="#bdldfp_decimalformatconfig-description"> Description </a>
27/// * <a href="#bdldfp_decimalformatconfig-attributes"> Attributes </a>
28///
29/// # Purpose {#bdldfp_decimalformatconfig-purpose}
30/// Provide an attribute class to configure decimal formatting.
31///
32/// # Classes {#bdldfp_decimalformatconfig-classes}
33///
34/// - bdldfp::DecimalFormatConfig: configuration for formatting functions
35///
36/// @see bdldfp_decimalutil
37///
38/// # Description {#bdldfp_decimalformatconfig-description}
39/// This component provides a single, simply constrained
40/// (value-semantic) attribute class, `bdldfp::DecimalFormatConfig`, that is
41/// used to configure various aspects of decimal value formatting.
42///
43/// ## Attributes {#bdldfp_decimalformatconfig-attributes}
44///
45///
46/// @code
47/// Name Type Default Simple Constraints
48/// --------- ------ --------------- ------------------
49/// style Style e_NATURAL none
50/// precision int 0 >= 0
51/// sign Sign e_NEGATIVE_ONLY none
52/// infinity string "inf" none
53/// nan string "nan" none
54/// snan string "snan" none
55/// point char '.' none
56/// exponent char 'e' none
57/// showpoint bool false none
58/// expwidth int 2 >= 1, <= 4
59/// @endcode
60/// * `style`: control how the decimal number is written. If `style` is
61/// `e_SCIENTIFIC`, the number is written as its sign, then a single digit,
62/// then the decimal point, then `precision` digits, then the `exponent`
63/// character, then a `-` or `+`, then an exponent with no leading zeroes
64/// (with a zero exponent written as `0`). If `style` is `e_FIXED`, the
65/// number is written as its sign, then one or more digits, then the decimal
66/// point, then `precision` digits. If the `precision` value equals `0` then
67/// `precision` digits and the decimal point are not written. If `style` is
68/// `e_NATURAL`, the number is written according to the description of
69/// `to-scientific-string` found in
70/// http://speleotrove.com/decimal/decarith.pdf (and no other specified
71/// formatting values are used, including precision).
72/// * `precision`: control how many digits are written after the decimal point
73/// if the decimal number is rendered in `e_FIXED` and `e_SCIENTIFIC`
74/// formats. Note that `precision` attribute is ignored in `e_NATURAL`
75/// format.
76/// * `sign`: control how the sign is output. If a decimal value has its sign
77/// bit set, a `-` is always written. Otherwise, if `sign` is
78/// `e_NEGATIVE_ONLY`, no sign is written. If it is `e_ALWAYS`, a `+` is
79/// written.
80/// * `infinity`: specify a string to output infinity value.
81/// * `nan`: specify a string to output NaN value.
82/// * `snan`: specify a string to output signaling NaN value.
83/// * `point`: specify the character to use for decimal points.
84/// * `exponent`: specify the character to use for exponent when `style` is
85/// `e_SCIENTIFIC` or `e_NATURAL`.
86/// * `showpoint`: specify whether a decimal point is always displayed.
87/// * `expwidth`: control the minimum number of digits used to write the
88/// exponent.
89/// @}
90/** @} */
91/** @} */
92
93/** @addtogroup bdl
94 * @{
95 */
96/** @addtogroup bdldfp
97 * @{
98 */
99/** @addtogroup bdldfp_decimalformatconfig
100 * @{
101 */
102
103#include <bsl_cstring.h>
104
105#include <bsls_assert.h>
106
107
108namespace bdldfp {
109
110 // =========================
111 // class DecimalFormatConfig
112 // =========================
113
114/// This attribute class characterizes how to configure certain behavior of
115/// `bdldfp::DecimalUtil::format` functions.
116///
117/// See @ref bdldfp_decimalformatconfig
119
120 public:
121 // TYPES
122 enum Sign {
123 e_NEGATIVE_ONLY, // no sign output when sign bit is not set
124 e_ALWAYS // output '+' when sign bit is not set
125 };
126
127 enum Style {
128 e_SCIENTIFIC, // output number in scientific notation
129 e_FIXED, // output number in fixed-format
130 e_NATURAL // output number in "to-scientific-string" format
131 // described in
132 // {http://speleotrove.com/decimal/decarith.pdf}
133 };
134
135 private:
136 // DATA
137 int d_precision; // precision (number of digits after point)
138 Style d_style; // formatting style
139 Sign d_sign; // sign character
140 const char *d_infinityText; // infinity representation
141 const char *d_nanText; // NaN representation
142 const char *d_sNanText; // signaling NaN representation
143 char d_decimalPoint; // decimal point character
144 char d_exponent; // exponent character
145 bool d_showpoint; // always show decimal
146 int d_expWidth; // minimum digits in exponent
147
148 // FRIENDS
149 friend bool operator==(const DecimalFormatConfig&,
150 const DecimalFormatConfig&);
151 friend bool operator!=(const DecimalFormatConfig&,
152 const DecimalFormatConfig&);
153
154 public:
155 // CREATORS
156
157 /// Create an object of this class having the (default) attribute
158 /// values:
159 /// @code
160 /// precision == 0
161 /// style == e_NATURAL
162 /// sign == e_NEGATIVE_ONLY
163 /// infinity == "inf"
164 /// nan == "nan"
165 /// snan == "snan"
166 /// point == '.'
167 /// exponent == 'e'
168 /// expwidth == 2
169 /// showpoint == false
170 /// @endcode
172
173 /// Create an object of this class having the specified `precision` to
174 /// control how many digits are written after a decimal point. The
175 /// behavior is undefined if `precision` is negative. Optionally
176 /// specify `style` to control how the number is written. If it is not
177 /// specified, `e_NATURAL` is used. Optionally specify `sign` to
178 /// control how the sign is output. If is not specified,
179 /// `e_NEGATIVE_ONLY` is used. Optionally specify `infinity` as a
180 /// string to output infinity value. If it is not specified, "inf" is
181 /// used. Optionally specify `nan` as a string to output NaN value. If
182 /// it is not specified, "nan" is used. Optionally specify `snan` as a
183 /// string to output signaling NaN value. If it is not specified,
184 /// "snan" is used. The behavior is undefined unless the pointers to
185 /// `infinity`, `nan` and `snan` remain valid for the lifetime of this
186 /// object. Optionally specify `point` as the character to use for
187 /// decimal points. If it is not specified, `.` is used. Optionally
188 /// specify `exponent` as the character to use for exponent. If it is
189 /// not specified, `e` is used. Optionally specify `showpoint` to force
190 /// a decimal point to always be written. Optionally specify `expWidth`
191 /// to force at least that many digits to be written for an exponent, up
192 /// to the number of digits in the largest supported exponent. If it is
193 /// not specified, 2 is used. The behavior is undefined unless
194 /// `expWidth` is 1, 2, 3, or 4. See the Attributes section under
195 /// @DESCRIPTION in the component-level documentation for information on
196 /// the class attributes.
197 explicit
201 const char *infinity = "inf",
202 const char *nan = "nan",
203 const char *snan = "snan",
204 char point = '.',
205 char exponent = 'e',
206 bool showpoint = false,
207 int expWidth = 2);
208
209 // MANIPULATORS
210
211 /// Set the `precision` attribute of this object to the specified
212 /// `value`. Behavior is undefined if `value` is negative.
213 void setPrecision (int value);
214
215 /// Set the `style` attribute of this object to the specified `value`.
216 void setStyle(Style value);
217
218 /// Set the `sign` attribute of this object to the specified `value`.
219 void setSign(Sign value);
220
221 /// Set the `infinity` attribute of this object to the specified
222 /// `value`. The behavior is undefined unless the pointer to the
223 /// `value` remains valid for the lifetime of this object.
224 void setInfinity(const char *value);
225
226 /// Set the `nan` attribute of this object to the specified `value`.
227 /// The behavior is undefined unless the pointer to the `value` remains
228 /// valid for the lifetime of this object.
229 void setNan(const char *value);
230
231 /// Set the `snan` attribute of this object to the specified `value`.
232 /// The behavior is undefined unless the pointer to the `value` remains
233 /// valid for the lifetime of this object.
234 void setSNan(const char *value);
235
236 /// Set the `point` attribute of this object to the specified `value`.
237 void setDecimalPoint(char value);
238
239 /// Set the `exponent` attribute of this object to the specified
240 /// `value`.
241 void setExponent(char value);
242
243 /// Set the `showpoint` attribute of this object to the specified
244 /// `value`.
245 void setShowpoint(bool value);
246
247 /// Set the `expwidth` attribute of this object to the specified
248 /// `value`. The behavior is undefined unless `value` is 1, 2, 3, or 4.
249 void setExpWidth(int value);
250
251 // ACCESSORS
252
253 /// Return the number of digits of precision in the outputs.
254 int precision() const;
255
256 /// Return the style of output format.
257 Style style() const;
258
259 /// Return the sign attribute.
260 Sign sign() const;
261
262 /// Return infinity string representation.
263 const char *infinity() const;
264
265 /// Return NaN string representation.
266 const char *nan() const;
267
268 /// Return sNaN string representation.
269 const char *sNan() const;
270
271 /// Return point character.
272 char decimalPoint() const;
273
274 /// Return exponent character.
275 char exponent() const;
276
277 /// Return the `showpoint` attribute.
278 bool showpoint() const;
279
280 /// Return the minimum exponent width.
281 int expWidth() const;
282};
283
284// FREE OPERATORS
285
286/// Return `true` if the specified `lhs` and `rhs` objects have the same
287/// value, and `false` otherwise. Two `DecimalFormatConfig` objects have
288/// the same value if each of their attributes (respectively) have the same
289/// value. Note that comparison of two string type attributes are done via
290/// 'bsl::strcmp() function.
291bool operator==(const DecimalFormatConfig& lhs,
292 const DecimalFormatConfig& rhs);
293
294/// Return `true` if the specified `lhs` and `rhs` objects do not have the
295/// same value, and `false` otherwise. Two `DecimalFormatConfig` objects
296/// do not have the same value if any of their attributes (respectively) do
297/// not have the same value. Note that comparison of two string type
298/// attributes are done via 'bsl::strcmp() function.
299bool operator!=(const DecimalFormatConfig& lhs,
300 const DecimalFormatConfig& rhs);
301
302
303// ============================================================================
304// INLINE DEFINITIONS
305// ============================================================================
306
307 // -------------------------
308 // class DecimalFormatConfig
309 // -------------------------
310
311// CREATORS
312inline
314 : d_precision(0)
315 , d_style(e_NATURAL)
316 , d_sign(e_NEGATIVE_ONLY)
317 , d_infinityText("inf")
318 , d_nanText("nan")
319 , d_sNanText("snan")
320 , d_decimalPoint('.')
321 , d_exponent('e')
322 , d_showpoint(false)
323 , d_expWidth(2)
324{
325}
326
327inline
329 Style style,
330 Sign sign,
331 const char *infinity,
332 const char *nan,
333 const char *snan,
334 char point,
335 char exponent,
336 bool showpoint,
337 int expWidth)
338 : d_precision(precision)
339 , d_style(style)
340 , d_sign(sign)
341 , d_infinityText(infinity)
342 , d_nanText(nan)
343 , d_sNanText(snan)
344 , d_decimalPoint(point)
345 , d_exponent(exponent)
346 , d_showpoint(showpoint)
347 , d_expWidth(expWidth)
348{
352 BSLS_ASSERT(snan);
353 BSLS_ASSERT(expWidth >= 1);
354 BSLS_ASSERT(expWidth <= 4);
355}
356
357// MANIPULATORS
358inline
360{
361 BSLS_ASSERT(value >= 0);
362 d_precision = value;
363}
364
365inline
367{
368 d_style = value;
369}
370
371inline
373{
374 d_sign = value;
375}
376
377inline
378void DecimalFormatConfig::setInfinity(const char *value)
379{
380 BSLS_ASSERT(value);
381 d_infinityText = value;
382}
383
384inline
385void DecimalFormatConfig::setNan(const char *value)
386{
387 BSLS_ASSERT(value);
388 d_nanText = value;
389}
390
391inline
392void DecimalFormatConfig::setSNan(const char *value)
393{
394 BSLS_ASSERT(value);
395 d_sNanText = value;
396}
397
398inline
400{
401 d_decimalPoint = value;
402}
403
404inline
406{
407 d_exponent = value;
408}
409
410inline
412{
413 d_showpoint = value;
414}
415
416
417inline
419{
420 BSLS_ASSERT(value >= 1);
421 BSLS_ASSERT(value <= 4);
422
423 d_expWidth = value;
424}
425
426// ACCESSORS
427inline
429{
430 return d_precision;
431}
432
433inline
435{
436 return d_style;
437}
438
439inline
441{
442 return d_sign;
443}
444
445inline
447{
448 return d_infinityText;
449}
450
451inline
452const char *DecimalFormatConfig::nan() const
453{
454 return d_nanText;
455}
456
457inline
458const char *DecimalFormatConfig::sNan() const
459{
460 return d_sNanText;
461}
462
463inline
465{
466 return d_decimalPoint;
467}
468
469inline
471{
472 return d_exponent;
473}
474
475inline
477{
478 return d_showpoint;
479}
480
481inline
483{
484 return d_expWidth;
485}
486} // close package namespace
487
488// FREE OPERATORS
489inline
490bool bdldfp::operator==(const DecimalFormatConfig& lhs,
491 const DecimalFormatConfig& rhs)
492{
493 return lhs.d_precision == rhs.d_precision &&
494 lhs.d_style == rhs.d_style &&
495 lhs.d_sign == rhs.d_sign &&
496 bsl::strcmp(lhs.d_infinityText, rhs.d_infinityText) == 0 &&
497 bsl::strcmp(lhs.d_nanText, rhs.d_nanText) == 0 &&
498 bsl::strcmp(lhs.d_sNanText, rhs.d_sNanText) == 0 &&
499 lhs.d_decimalPoint == rhs.d_decimalPoint &&
500 lhs.d_exponent == rhs.d_exponent &&
501 lhs.d_showpoint == rhs.d_showpoint &&
502 lhs.d_expWidth == rhs.d_expWidth;
503}
504
505inline
506bool bdldfp::operator!=(const DecimalFormatConfig& lhs,
507 const DecimalFormatConfig& rhs)
508{
509 return lhs.d_precision != rhs.d_precision ||
510 lhs.d_style != rhs.d_style ||
511 lhs.d_sign != rhs.d_sign ||
512 bsl::strcmp(lhs.d_infinityText, rhs.d_infinityText) ||
513 bsl::strcmp(lhs.d_nanText, rhs.d_nanText) ||
514 bsl::strcmp(lhs.d_sNanText, rhs.d_sNanText) ||
515 lhs.d_decimalPoint != rhs.d_decimalPoint ||
516 lhs.d_exponent != rhs.d_exponent ||
517 lhs.d_showpoint != rhs.d_showpoint ||
518 lhs.d_expWidth != rhs.d_expWidth;
519}
520
521
522
523#endif
524
525// ----------------------------------------------------------------------------
526// Copyright 2017 Bloomberg Finance L.P.
527//
528// Licensed under the Apache License, Version 2.0 (the "License");
529// you may not use this file except in compliance with the License.
530// You may obtain a copy of the License at
531//
532// http://www.apache.org/licenses/LICENSE-2.0
533//
534// Unless required by applicable law or agreed to in writing, software
535// distributed under the License is distributed on an "AS IS" BASIS,
536// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
537// See the License for the specific language governing permissions and
538// limitations under the License.
539// ----------------------------- END-OF-FILE ----------------------------------
540
541/** @} */
542/** @} */
543/** @} */
Definition bdldfp_decimalformatconfig.h:118
Style
Definition bdldfp_decimalformatconfig.h:127
@ e_FIXED
Definition bdldfp_decimalformatconfig.h:129
@ e_NATURAL
Definition bdldfp_decimalformatconfig.h:130
@ e_SCIENTIFIC
Definition bdldfp_decimalformatconfig.h:128
bool showpoint() const
Return the showpoint attribute.
Definition bdldfp_decimalformatconfig.h:476
char decimalPoint() const
Return point character.
Definition bdldfp_decimalformatconfig.h:464
friend bool operator!=(const DecimalFormatConfig &, const DecimalFormatConfig &)
Sign sign() const
Return the sign attribute.
Definition bdldfp_decimalformatconfig.h:440
void setNan(const char *value)
Definition bdldfp_decimalformatconfig.h:385
void setPrecision(int value)
Definition bdldfp_decimalformatconfig.h:359
void setShowpoint(bool value)
Definition bdldfp_decimalformatconfig.h:411
friend bool operator==(const DecimalFormatConfig &, const DecimalFormatConfig &)
void setExpWidth(int value)
Definition bdldfp_decimalformatconfig.h:418
void setSNan(const char *value)
Definition bdldfp_decimalformatconfig.h:392
Style style() const
Return the style of output format.
Definition bdldfp_decimalformatconfig.h:434
DecimalFormatConfig()
Definition bdldfp_decimalformatconfig.h:313
const char * sNan() const
Return sNaN string representation.
Definition bdldfp_decimalformatconfig.h:458
void setStyle(Style value)
Set the style attribute of this object to the specified value.
Definition bdldfp_decimalformatconfig.h:366
int precision() const
Return the number of digits of precision in the outputs.
Definition bdldfp_decimalformatconfig.h:428
void setInfinity(const char *value)
Definition bdldfp_decimalformatconfig.h:378
void setSign(Sign value)
Set the sign attribute of this object to the specified value.
Definition bdldfp_decimalformatconfig.h:372
void setDecimalPoint(char value)
Set the point attribute of this object to the specified value.
Definition bdldfp_decimalformatconfig.h:399
const char * infinity() const
Return infinity string representation.
Definition bdldfp_decimalformatconfig.h:446
Sign
Definition bdldfp_decimalformatconfig.h:122
@ e_NEGATIVE_ONLY
Definition bdldfp_decimalformatconfig.h:123
@ e_ALWAYS
Definition bdldfp_decimalformatconfig.h:124
void setExponent(char value)
Definition bdldfp_decimalformatconfig.h:405
int expWidth() const
Return the minimum exponent width.
Definition bdldfp_decimalformatconfig.h:482
const char * nan() const
Return NaN string representation.
Definition bdldfp_decimalformatconfig.h:452
char exponent() const
Return exponent character.
Definition bdldfp_decimalformatconfig.h:470
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdldfp_decimal.h:712
bool operator!=(Decimal32 lhs, Decimal32 rhs)
bool operator==(Decimal32 lhs, Decimal32 rhs)