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