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(int value);
50 /// Formats an integer value in decimal notation.
51
52 static std::string format(int value, 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(int value, 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(int value, 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(int value, int width, 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(unsigned value);
77 /// Formats an unsigned int value in decimal notation.
78
79 static std::string format(unsigned value, 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(unsigned int value, 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(unsigned value, 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(unsigned value, int width, 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(long value);
104 /// Formats a long value in decimal notation.
105
106 static std::string format(long value, 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(long value, 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(long value, 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(long value, int width, 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(unsigned long value);
131 /// Formats an unsigned long value in decimal notation.
132
133 static std::string format(unsigned long value, 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(unsigned long value, 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(unsigned long value, 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(unsigned long value, int width, 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(Int64 value);
158 /// Formats a 64-bit integer value in decimal notation.
159
160 static std::string format(Int64 value, 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(Int64 value, 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(Int64 value, 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(Int64 value, int width, 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(UInt64 value);
183 /// Formats an unsigned 64-bit integer value in decimal notation.
184
185 static std::string format(UInt64 value, 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(UInt64 value, 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(UInt64 value, 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(UInt64 value, int width, 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(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(float value, 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(float value, int width, 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(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(double value, 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(double value, int width, 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(bool value, 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, int value);
241 /// Formats an integer value in decimal notation.
242
243 static void append(std::string& str, int value, 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, int value, 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, 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, int value, 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, unsigned value);
264 /// Formats an unsigned int value in decimal notation.
265
266 static void append(std::string& str, unsigned value, 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, unsigned int value, 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, unsigned value);
277 /// Formats an unsigned int value in hexadecimal notation.
278
279 static void appendHex(std::string& str, unsigned value, 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, long value);
287 /// Formats a long value in decimal notation.
288
289 static void append(std::string& str, long value, 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, long value, 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, 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, long value, 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, unsigned long value);
310 /// Formats an unsigned long value in decimal notation.
311
312 static void append(std::string& str, unsigned long value, 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, unsigned long value, 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, unsigned long value);
323 /// Formats an unsigned long value in hexadecimal notation.
324
325 static void appendHex(std::string& str, unsigned long value, 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, Int64 value);
333 /// Formats a 64-bit integer value in decimal notation.
334
335 static void append(std::string& str, Int64 value, 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, Int64 value, 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, 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, Int64 value, 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, UInt64 value);
355 /// Formats an unsigned 64-bit integer value in decimal notation.
356
357 static void append(std::string& str, UInt64 value, 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, UInt64 value, 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, UInt64 value);
367 /// Formats a 64-bit integer value in hexadecimal notation.
368
369 static void appendHex(std::string& str, UInt64 value, 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, 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, float value, 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, float value, int width, 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, 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, double value, 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, double value, int width, 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(int value)
414{
415 std::string result;
416 intToStr(value, 10, result);
417 return result;
418}
419
420
421inline std::string NumberFormatter::format(int value, int width)
422{
423 std::string result;
424 intToStr(value, 10, result, false, width, ' ');
425 return result;
426}
427
428
429inline std::string NumberFormatter::format0(int value, int width)
430{
431 std::string result;
432 intToStr(value, 10, result, false, width, '0');
433 return result;
434}
435
436
437inline std::string NumberFormatter::formatHex(int value, bool prefix)
438{
439 std::string result;
440 uIntToStr(static_cast<unsigned int>(value), 0x10, result, prefix);
441 return result;
442}
443
444
445inline std::string NumberFormatter::formatHex(int value, int width, bool prefix)
446{
447 std::string result;
448 uIntToStr(static_cast<unsigned int>(value), 0x10, result, prefix, width, '0');
449 return result;
450}
451
452
453inline std::string NumberFormatter::format(unsigned value)
454{
455 std::string result;
456 uIntToStr(value, 10, result);
457 return result;
458}
459
460
461inline std::string NumberFormatter::format(unsigned value, int width)
462{
463 std::string result;
464 uIntToStr(value, 10, result, false, width, ' ');
465 return result;
466}
467
468
469inline std::string NumberFormatter::format0(unsigned int value, int width)
470{
471 std::string result;
472 uIntToStr(value, 10, result, false, width, '0');
473 return result;
474}
475
476
477inline std::string NumberFormatter::formatHex(unsigned value, bool prefix)
478{
479 std::string result;
480 uIntToStr(value, 0x10, result, prefix);
481 return result;
482}
483
484
485inline std::string NumberFormatter::formatHex(unsigned value, int width, bool prefix)
486{
487 std::string result;
488 uIntToStr(value, 0x10, result, prefix, width, '0');
489 return result;
490}
491
492
493#ifndef POCO_LONG_IS_64_BIT
494
495
496inline std::string NumberFormatter::format(long value)
497{
498 std::string result;
499 intToStr(value, 10, result);
500 return result;
501}
502
503
504inline std::string NumberFormatter::format(long value, int width)
505{
506 std::string result;
507 intToStr(value, 10, result, false, width, ' ');
508 return result;
509}
510
511
512inline std::string NumberFormatter::format0(long value, int width)
513{
514 std::string result;
515 intToStr(value, 10, result, false, width, '0');
516 return result;
517}
518
519
520inline std::string NumberFormatter::formatHex(long value, bool prefix)
521{
522 std::string result;
523 uIntToStr(static_cast<unsigned long>(value), 0x10, result, prefix);
524 return result;
525}
526
527
528inline std::string NumberFormatter::formatHex(long value, int width, bool prefix)
529{
530 std::string result;
531 uIntToStr(static_cast<unsigned long>(value), 0x10, result, prefix, width, '0');
532 return result;
533}
534
535
536inline std::string NumberFormatter::format(unsigned long value)
537{
538 std::string result;
539 uIntToStr(value, 10, result);
540 return result;
541}
542
543
544inline std::string NumberFormatter::format(unsigned long value, int width)
545{
546 std::string result;
547 uIntToStr(value, 10, result, false, width, ' ');
548 return result;
549}
550
551
552inline std::string NumberFormatter::format0(unsigned long value, int width)
553{
554 std::string result;
555 uIntToStr(value, 10, result, false, width, '0');
556 return result;
557}
558
559
560inline std::string NumberFormatter::formatHex(unsigned long value, bool prefix)
561{
562 std::string result;
563 uIntToStr(value, 0x10, result, prefix);
564 return result;
565}
566
567
568inline std::string NumberFormatter::formatHex(unsigned long value, int width, bool prefix)
569{
570 std::string result;
571 uIntToStr(value, 0x10, result, prefix, width, '0');
572 return result;
573}
574
575
576#endif // POCO_LONG_IS_64_BIT
577
578
579inline std::string NumberFormatter::format(Int64 value)
580{
581 std::string result;
582 intToStr(value, 10, result);
583 return result;
584}
585
586
587inline std::string NumberFormatter::format(Int64 value, int width)
588{
589 std::string result;
590 intToStr(value, 10, result, false, width, ' ');
591 return result;
592}
593
594
595inline std::string NumberFormatter::format0(Int64 value, int width)
596{
597 std::string result;
598 intToStr(value, 10, result, false, width, '0');
599 return result;
600}
601
602
603inline std::string NumberFormatter::formatHex(Int64 value, bool prefix)
604{
605 std::string result;
606 uIntToStr(static_cast<UInt64>(value), 0x10, result, prefix);
607 return result;
608}
609
610
611inline std::string NumberFormatter::formatHex(Int64 value, int width, bool prefix)
612{
613 std::string result;
614 uIntToStr(static_cast<UInt64>(value), 0x10, result, prefix, width, '0');
615 return result;
616}
617
618
619inline std::string NumberFormatter::format(UInt64 value)
620{
621 std::string result;
622 uIntToStr(value, 10, result);
623 return result;
624}
625
626
627inline std::string NumberFormatter::format(UInt64 value, int width)
628{
629 std::string result;
630 uIntToStr(value, 10, result, false, width, ' ');
631 return result;
632}
633
634
635inline std::string NumberFormatter::format0(UInt64 value, int width)
636{
637 std::string result;
638 uIntToStr(value, 10, result, false, width, '0');
639 return result;
640}
641
642
643inline std::string NumberFormatter::formatHex(UInt64 value, bool prefix)
644{
645 std::string result;
646 uIntToStr(value, 0x10, result, prefix);
647 return result;
648}
649
650
651inline std::string NumberFormatter::formatHex(UInt64 value, int width, bool prefix)
652{
653 std::string result;
654 uIntToStr(value, 0x10, result, prefix, width, '0');
655 return result;
656}
657
658
659inline std::string NumberFormatter::format(float value)
660{
661 char buffer[POCO_MAX_FLT_STRING_LEN];
662 floatToStr(buffer, POCO_MAX_FLT_STRING_LEN, value);
663 return std::string(buffer);
664}
665
666
667inline std::string NumberFormatter::format(float value, int precision)
668{
669 char buffer[POCO_MAX_FLT_STRING_LEN];
670 floatToFixedStr(buffer, POCO_MAX_FLT_STRING_LEN, value, precision);
671 return std::string(buffer);
672}
673
674
675inline std::string NumberFormatter::format(float value, int width, int precision)
676{
677 std::string result;
678 floatToFixedStr(result, value, precision, width);
679 return result;
680}
681
682
683inline std::string NumberFormatter::format(double value)
684{
685 char buffer[POCO_MAX_FLT_STRING_LEN];
686 doubleToStr(buffer, POCO_MAX_FLT_STRING_LEN, value);
687 return std::string(buffer);
688}
689
690
691inline std::string NumberFormatter::format(double value, int precision)
692{
693 char buffer[POCO_MAX_FLT_STRING_LEN];
694 doubleToFixedStr(buffer, POCO_MAX_FLT_STRING_LEN, value, precision);
695 return std::string(buffer);
696}
697
698
699inline std::string NumberFormatter::format(double value, int width, int precision)
700{
701 std::string result;
702 doubleToFixedStr(result, value, precision, width);
703 return result;
704}
705
706
707inline std::string NumberFormatter::format(const void* ptr)
708{
709 std::string result;
710 append(result, ptr);
711 return result;
712}
713
714
715} // namespace Poco
716
717
718#endif // Foundation_NumberFormatter_INCLUDED
719