1//
2// NumberFormatter.h
3//
4// Library: Foundation
5// Package: Core
6// Module: NumberFormatter
7//
8// Definition of the NumberFormatter class.
9//
10// Copyright (c) 2004-2008, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// SPDX-License-Identifier: BSL-1.0
14//
15
16
17#ifndef Foundation_NumberFormatter_INCLUDED
18#define Foundation_NumberFormatter_INCLUDED
19
20
21#include "Poco/Foundation.h"
22#include "Poco/NumericString.h"
23
24
25namespace Poco {
26
27
28class Foundation_API NumberFormatter
29 /// The NumberFormatter class provides static methods
30 /// for formatting numeric values into strings.
31 ///
32 /// There are two kind of static member functions:
33 /// * format* functions return a std::string containing
34 /// the formatted value.
35 /// * append* functions append the formatted value to
36 /// an existing string.
37{
38public:
39 enum BoolFormat
40 {
41 FMT_TRUE_FALSE,
42 FMT_YES_NO,
43 FMT_ON_OFF
44 };
45
46 static const unsigned NF_MAX_INT_STRING_LEN = 32; // increase for 64-bit binary formatting support
47 static const unsigned NF_MAX_FLT_STRING_LEN = POCO_MAX_FLT_STRING_LEN;
48
49 static std::string format(const int value);
50 /// Formats an integer value in decimal notation.
51
52 static std::string format(const int value, const int width);
53 /// Formats an integer value in decimal notation,
54 /// right justified in a field having at least
55 /// the specified width.
56
57 static std::string format0(const int value, const int width);
58 /// Formats an integer value in decimal notation,
59 /// right justified and zero-padded in a field
60 /// having at least the specified width.
61
62 static std::string formatHex(const int value, const bool prefix = false);
63 /// Formats an int value in hexadecimal notation.
64 /// If prefix is true, "0x" prefix is prepended to the
65 /// resulting string.
66 /// The value is treated as unsigned.
67
68 static std::string formatHex(const int value, const int width, const bool prefix = false);
69 /// Formats a int value in hexadecimal notation,
70 /// right justified and zero-padded in
71 /// a field having at least the specified width.
72 /// If prefix is true, "0x" prefix is prepended to the
73 /// resulting string.
74 /// The value is treated as unsigned.
75
76 static std::string format(const unsigned value);
77 /// Formats an unsigned int value in decimal notation.
78
79 static std::string format(const unsigned value, const int width);
80 /// Formats an unsigned long int in decimal notation,
81 /// right justified in a field having at least the
82 /// specified width.
83
84 static std::string format0(const unsigned int value, const int width);
85 /// Formats an unsigned int value in decimal notation,
86 /// right justified and zero-padded in a field having at
87 /// least the specified width.
88
89 static std::string formatHex(const unsigned value, const bool prefix = false);
90 /// Formats an unsigned int value in hexadecimal notation.
91 /// If prefix is true, "0x" prefix is prepended to the
92 /// resulting string.
93
94 static std::string formatHex(const unsigned value, const int width, const bool prefix = false);
95 /// Formats a int value in hexadecimal notation,
96 /// right justified and zero-padded in
97 /// a field having at least the specified width.
98 /// If prefix is true, "0x" prefix is prepended to the
99 /// resulting string.
100
101#ifndef POCO_LONG_IS_64_BIT
102
103 static std::string format(const long value);
104 /// Formats a long value in decimal notation.
105
106 static std::string format(const long value, const int width);
107 /// Formats a long value in decimal notation,
108 /// right justified in a field having at least the
109 /// specified width.
110
111 static std::string format0(const long value, const int width);
112 /// Formats a long value in decimal notation,
113 /// right justified and zero-padded in a field
114 /// having at least the specified width.
115
116 static std::string formatHex(const long value, const bool prefix = false);
117 /// Formats an unsigned long value in hexadecimal notation.
118 /// If prefix is true, "0x" prefix is prepended to the
119 /// resulting string.
120 /// The value is treated as unsigned.
121
122 static std::string formatHex(const long value, const int width, const bool prefix = false);
123 /// Formats an unsigned long value in hexadecimal notation,
124 /// right justified and zero-padded in a field having at least the
125 /// specified width.
126 /// If prefix is true, "0x" prefix is prepended to the
127 /// resulting string.
128 /// The value is treated as unsigned.
129
130 static std::string format(const unsigned long value);
131 /// Formats an unsigned long value in decimal notation.
132
133 static std::string format(const unsigned long value, const int width);
134 /// Formats an unsigned long value in decimal notation,
135 /// right justified in a field having at least the specified
136 /// width.
137
138 static std::string format0(const unsigned long value, const int width);
139 /// Formats an unsigned long value in decimal notation,
140 /// right justified and zero-padded
141 /// in a field having at least the specified width.
142
143 static std::string formatHex(const unsigned long value, const bool prefix = false);
144 /// Formats an unsigned long value in hexadecimal notation.
145 /// If prefix is true, "0x" prefix is prepended to the
146 /// resulting string.
147
148 static std::string formatHex(const unsigned long value, const int width, const bool prefix = false);
149 /// Formats an unsigned long value in hexadecimal notation,
150 /// right justified and zero-padded in a field having at least the
151 /// specified width.
152 /// If prefix is true, "0x" prefix is prepended to the
153 /// resulting string.
154
155#endif // POCO_LONG_IS_64_BIT
156
157 static std::string format(const Int64 value);
158 /// Formats a 64-bit integer value in decimal notation.
159
160 static std::string format(const Int64 value, const int width);
161 /// Formats a 64-bit integer value in decimal notation,
162 /// right justified in a field having at least the specified width.
163
164 static std::string format0(const Int64 value, const int width);
165 /// Formats a 64-bit integer value in decimal notation,
166 /// right justified and zero-padded in a field having at least
167 /// the specified width.
168
169 static std::string formatHex(const Int64 value, const bool prefix = false);
170 /// Formats a 64-bit integer value in hexadecimal notation.
171 /// If prefix is true, "0x" prefix is prepended to the
172 /// resulting string.
173 /// The value is treated as unsigned.
174
175 static std::string formatHex(const Int64 value, const int width, const bool prefix = false);
176 /// Formats a 64-bit integer value in hexadecimal notation,
177 /// right justified and zero-padded in a field having at least
178 /// the specified width.
179 /// The value is treated as unsigned.
180 /// If prefix is true, "0x" prefix is prepended to the resulting string.
181
182 static std::string format(const UInt64 value);
183 /// Formats an unsigned 64-bit integer value in decimal notation.
184
185 static std::string format(const UInt64 value, const int width);
186 /// Formats an unsigned 64-bit integer value in decimal notation,
187 /// right justified in a field having at least the specified width.
188
189 static std::string format0(const UInt64 value, const int width);
190 /// Formats an unsigned 64-bit integer value in decimal notation,
191 /// right justified and zero-padded in a field having at least the
192 /// specified width.
193
194 static std::string formatHex(const UInt64 value, const bool prefix = false);
195 /// Formats a 64-bit integer value in hexadecimal notation.
196 /// If prefix is true, "0x" prefix is prepended to the
197 /// resulting string.
198
199 static std::string formatHex(const UInt64 value, const int width, const bool prefix = false);
200 /// Formats a 64-bit integer value in hexadecimal notation,
201 /// right justified and zero-padded in a field having at least
202 /// the specified width. If prefix is true, "0x" prefix is
203 /// prepended to the resulting string.
204
205 static std::string format(const float value);
206 /// Formats a float value in decimal floating-point notation,
207 /// according to std::printf's %g format with a precision of 8 fractional digits.
208
209 static std::string format(const float value, const int precision);
210 /// Formats a double value in decimal floating-point notation,
211 /// according to std::printf's %f format with the given precision.
212
213 static std::string format(const float value, const int width, const int precision);
214 /// Formats a double value in decimal floating-point notation,
215 /// right justified in a field of the specified width,
216 /// with the number of fractional digits given in precision.
217
218 static std::string format(const double value);
219 /// Formats a double value in decimal floating-point notation,
220 /// according to std::printf's %g format with a precision of 16 fractional digits.
221
222 static std::string format(const double value, const int precision);
223 /// Formats a double value in decimal floating-point notation,
224 /// according to std::printf's %f format with the given precision.
225
226 static std::string format(const double value, const int width, const int precision);
227 /// Formats a double value in decimal floating-point notation,
228 /// right justified in a field of the specified width,
229 /// with the number of fractional digits given in precision.
230
231 static std::string format(const void* ptr);
232 /// Formats a pointer in an eight (32-bit architectures) or
233 /// sixteen (64-bit architectures) characters wide
234 /// field in hexadecimal notation.
235
236 static std::string format(const bool value, const BoolFormat format = FMT_TRUE_FALSE);
237 /// Formats a bool value in decimal/text notation,
238 /// according to format parameter.
239
240 static void append(std::string& str, const int value);
241 /// Formats an integer value in decimal notation.
242
243 static void append(std::string& str, const int value, const int width);
244 /// Formats an integer value in decimal notation,
245 /// right justified in a field having at least
246 /// the specified width.
247
248 static void append0(std::string& str, const int value, const int width);
249 /// Formats an integer value in decimal notation,
250 /// right justified and zero-padded in a field
251 /// having at least the specified width.
252
253 static void appendHex(std::string& str, const int value);
254 /// Formats an int value in hexadecimal notation.
255 /// The value is treated as unsigned.
256
257 static void appendHex(std::string& str, const int value, const int width);
258 /// Formats a int value in hexadecimal notation,
259 /// right justified and zero-padded in
260 /// a field having at least the specified width.
261 /// The value is treated as unsigned.
262
263 static void append(std::string& str, const unsigned value);
264 /// Formats an unsigned int value in decimal notation.
265
266 static void append(std::string& str, const unsigned value, const int width);
267 /// Formats an unsigned long int in decimal notation,
268 /// right justified in a field having at least the
269 /// specified width.
270
271 static void append0(std::string& str, const unsigned int value, const int width);
272 /// Formats an unsigned int value in decimal notation,
273 /// right justified and zero-padded in a field having at
274 /// least the specified width.
275
276 static void appendHex(std::string& str, const unsigned value);
277 /// Formats an unsigned int value in hexadecimal notation.
278
279 static void appendHex(std::string& str, const unsigned value, const int width);
280 /// Formats a int value in hexadecimal notation,
281 /// right justified and zero-padded in
282 /// a field having at least the specified width.
283
284#ifndef POCO_LONG_IS_64_BIT
285
286 static void append(std::string& str, const long value);
287 /// Formats a long value in decimal notation.
288
289 static void append(std::string& str, const long value, const int width);
290 /// Formats a long value in decimal notation,
291 /// right justified in a field having at least the
292 /// specified width.
293
294 static void append0(std::string& str, const long value, const int width);
295 /// Formats a long value in decimal notation,
296 /// right justified and zero-padded in a field
297 /// having at least the specified width.
298
299 static void appendHex(std::string& str, const long value);
300 /// Formats an unsigned long value in hexadecimal notation.
301 /// The value is treated as unsigned.
302
303 static void appendHex(std::string& str, const long value, const int width);
304 /// Formats an unsigned long value in hexadecimal notation,
305 /// right justified and zero-padded in a field having at least the
306 /// specified width.
307 /// The value is treated as unsigned.
308
309 static void append(std::string& str, const unsigned long value);
310 /// Formats an unsigned long value in decimal notation.
311
312 static void append(std::string& str, const unsigned long value, const int width);
313 /// Formats an unsigned long value in decimal notation,
314 /// right justified in a field having at least the specified
315 /// width.
316
317 static void append0(std::string& str, const unsigned long value, const int width);
318 /// Formats an unsigned long value in decimal notation,
319 /// right justified and zero-padded
320 /// in a field having at least the specified width.
321
322 static void appendHex(std::string& str, const unsigned long value);
323 /// Formats an unsigned long value in hexadecimal notation.
324
325 static void appendHex(std::string& str, const unsigned long value, const int width);
326 /// Formats an unsigned long value in hexadecimal notation,
327 /// right justified and zero-padded in a field having at least the
328 /// specified width.
329
330#endif // POCO_LONG_IS_64_BIT
331
332 static void append(std::string& str, const Int64 value);
333 /// Formats a 64-bit integer value in decimal notation.
334
335 static void append(std::string& str, const Int64 value, const int width);
336 /// Formats a 64-bit integer value in decimal notation,
337 /// right justified in a field having at least the specified width.
338
339 static void append0(std::string& str, const Int64 value, const int width);
340 /// Formats a 64-bit integer value in decimal notation,
341 /// right justified and zero-padded in a field having at least
342 /// the specified width.
343
344 static void appendHex(std::string& str, const Int64 value);
345 /// Formats a 64-bit integer value in hexadecimal notation.
346 /// The value is treated as unsigned.
347
348 static void appendHex(std::string& str, const Int64 value, const int width);
349 /// Formats a 64-bit integer value in hexadecimal notation,
350 /// right justified and zero-padded in a field having at least
351 /// the specified width.
352 /// The value is treated as unsigned.
353
354 static void append(std::string& str, const UInt64 value);
355 /// Formats an unsigned 64-bit integer value in decimal notation.
356
357 static void append(std::string& str, const UInt64 value, const int width);
358 /// Formats an unsigned 64-bit integer value in decimal notation,
359 /// right justified in a field having at least the specified width.
360
361 static void append0(std::string& str, const UInt64 value, const int width);
362 /// Formats an unsigned 64-bit integer value in decimal notation,
363 /// right justified and zero-padded in a field having at least the
364 /// specified width.
365
366 static void appendHex(std::string& str, const UInt64 value);
367 /// Formats a 64-bit integer value in hexadecimal notation.
368
369 static void appendHex(std::string& str, const UInt64 value, const int width);
370 /// Formats a 64-bit integer value in hexadecimal notation,
371 /// right justified and zero-padded in a field having at least
372 /// the specified width.
373
374 static void append(std::string& str, const float value);
375 /// Formats a float value in decimal floating-point notation,
376 /// according to std::printf's %g format with a precision of 8 fractional digits.
377
378 static void append(std::string& str, const float value, const int precision);
379 /// Formats a double value in decimal floating-point notation,
380 /// according to std::printf's %f format with the given precision.
381
382 static void append(std::string& str, const float value, const int width, const int precision);
383 /// Formats a double value in decimal floating-point notation,
384 /// right justified in a field of the specified width,
385 /// with the number of fractional digits given in precision.
386
387 static void append(std::string& str, const double value);
388 /// Formats a double value in decimal floating-point notation,
389 /// according to std::printf's %g format with a precision of 16 fractional digits.
390
391 static void append(std::string& str, const double value, const int precision);
392 /// Formats a double value in decimal floating-point notation,
393 /// according to std::printf's %f format with the given precision.
394
395 static void append(std::string& str, const double value, const int width, const int precision);
396 /// Formats a double value in decimal floating-point notation,
397 /// right justified in a field of the specified width,
398 /// with the number of fractional digits given in precision.
399
400 static void append(std::string& str, const void* ptr);
401 /// Formats a pointer in an eight (32-bit architectures) or
402 /// sixteen (64-bit architectures) characters wide
403 /// field in hexadecimal notation.
404
405private:
406};
407
408
409//
410// inlines
411//
412
413inline std::string NumberFormatter::format(const int value)
414{
415 return intToStr(value, 10);
416}
417
418
419inline std::string NumberFormatter::format(const int value, const int width)
420{
421 return intToStr(value, 10, false, width, ' ');
422}
423
424
425inline std::string NumberFormatter::format0(const int value, const int width)
426{
427 return intToStr(value, 10, false, width, '0');
428}
429
430
431inline std::string NumberFormatter::formatHex(const int value, const bool prefix)
432{
433 return uIntToStr(static_cast<unsigned int>(value), 0x10, prefix);
434}
435
436
437inline std::string NumberFormatter::formatHex(const int value, const int width, const bool prefix)
438{
439 return uIntToStr(static_cast<unsigned int>(value), 0x10, prefix, width, '0');
440}
441
442
443inline std::string NumberFormatter::format(const unsigned value)
444{
445 return uIntToStr(value, 10);
446}
447
448
449inline std::string NumberFormatter::format(const unsigned value, const int width)
450{
451 return uIntToStr(value, 10, false, width, ' ');
452}
453
454
455inline std::string NumberFormatter::format0(const unsigned int value, const int width)
456{
457 return uIntToStr(value, 10, false, width, '0');
458}
459
460
461inline std::string NumberFormatter::formatHex(const unsigned value, const bool prefix)
462{
463 return uIntToStr(value, 0x10, prefix);
464}
465
466
467inline std::string NumberFormatter::formatHex(const unsigned value, const int width, const bool prefix)
468{
469 return uIntToStr(value, 0x10, prefix, width, '0');
470}
471
472
473#ifndef POCO_LONG_IS_64_BIT
474
475
476inline std::string NumberFormatter::format(const long value)
477{
478 return intToStr(value, 10);
479}
480
481
482inline std::string NumberFormatter::format(const long value, const int width)
483{
484 return intToStr(value, 10, false, width, ' ');
485}
486
487
488inline std::string NumberFormatter::format0(const long value, const int width)
489{
490 return intToStr(value, 10, false, width, '0');
491}
492
493
494inline std::string NumberFormatter::formatHex(const long value, const bool prefix)
495{
496 return uIntToStr(static_cast<unsigned long>(value), 0x10, prefix);
497}
498
499
500inline std::string NumberFormatter::formatHex(const long value, const int width, const bool prefix)
501{
502 return uIntToStr(static_cast<unsigned long>(value), 0x10, prefix, width, '0');
503}
504
505
506inline std::string NumberFormatter::format(const unsigned long value)
507{
508 return uIntToStr(value, 10);
509}
510
511
512inline std::string NumberFormatter::format(const unsigned long value, const int width)
513{
514 return uIntToStr(value, 10, false, width, ' ');
515}
516
517
518inline std::string NumberFormatter::format0(const unsigned long value, const int width)
519{
520 return uIntToStr(value, 10, false, width, '0');
521}
522
523
524inline std::string NumberFormatter::formatHex(const unsigned long value, const bool prefix)
525{
526 return uIntToStr(value, 0x10, prefix);
527}
528
529
530inline std::string NumberFormatter::formatHex(const unsigned long value, const int width, const bool prefix)
531{
532 return uIntToStr(value, 0x10, prefix, width, '0');
533}
534
535
536#endif // POCO_LONG_IS_64_BIT
537
538
539inline std::string NumberFormatter::format(const Int64 value)
540{
541 return intToStr(value, 10);
542}
543
544
545inline std::string NumberFormatter::format(const Int64 value, const int width)
546{
547 return intToStr(value, 10, false, width, ' ');
548}
549
550
551inline std::string NumberFormatter::format0(const Int64 value, const int width)
552{
553 return intToStr(value, 10, false, width, '0');
554}
555
556
557inline std::string NumberFormatter::formatHex(const Int64 value, const bool prefix)
558{
559 return uIntToStr(static_cast<UInt64>(value), 0x10, prefix);
560}
561
562
563inline std::string NumberFormatter::formatHex(const Int64 value, const int width, const bool prefix)
564{
565 return uIntToStr(static_cast<UInt64>(value), 0x10, prefix, width, '0');
566}
567
568
569inline std::string NumberFormatter::format(const UInt64 value)
570{
571 return uIntToStr(value, 10);
572}
573
574
575inline std::string NumberFormatter::format(const UInt64 value, const int width)
576{
577 return uIntToStr(value, 10, false, width, ' ');
578}
579
580
581inline std::string NumberFormatter::format0(const UInt64 value, const int width)
582{
583 return uIntToStr(value, 10, false, width, '0');
584}
585
586
587inline std::string NumberFormatter::formatHex(const UInt64 value, const bool prefix)
588{
589 return uIntToStr(value, 0x10, prefix);
590}
591
592
593inline std::string NumberFormatter::formatHex(UInt64 value, int width, bool prefix)
594{
595 return uIntToStr(value, 0x10, prefix, width, '0');
596}
597
598
599inline std::string NumberFormatter::format(const float value)
600{
601 return floatToStr(value);
602}
603
604
605inline std::string NumberFormatter::format(const float value, const int precision)
606{
607 return floatToStr(value, precision);
608}
609
610
611inline std::string NumberFormatter::format(const float value, const int width, const int precision)
612{
613 return floatToFixedStr(value, precision, width);
614}
615
616
617inline std::string NumberFormatter::format(const double value)
618{
619 return doubleToStr(value);
620}
621
622
623inline std::string NumberFormatter::format(const double value, const int precision)
624{
625 return doubleToFixedStr(value, precision);
626}
627
628
629inline std::string NumberFormatter::format(const double value, const int width, const int precision)
630{
631 return doubleToFixedStr(value, precision, width);
632}
633
634
635inline std::string NumberFormatter::format(const void* ptr)
636{
637 std::string result;
638 append(result, ptr);
639 return result;
640}
641
642
643} // namespace Poco
644
645
646#endif // Foundation_NumberFormatter_INCLUDED
647