1 package org.djunits.value.formatter;
2
3 /**
4 * Formatter of values with width and precision.
5 * <p>
6 * Copyright (c) 2015-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
7 * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
8 * </p>
9 * @version $Revision: 954 $, $LastChangedDate: 2022-01-10 03:42:57 +0100 (Mon, 10 Jan 2022) $, by $Author: averbraeck $,
10 * initial version 11 sep. 2015 <br>
11 * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
12 * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
13 */
14 public final class Formatter
15 {
16 /**
17 * This class shall never be instantiated.
18 */
19 private Formatter()
20 {
21 // Prevent instantiation of this class
22 }
23
24 /** Default number of fraction digits. */
25 public static final int DEFAULTPRECISION = 3;
26
27 /**
28 * Build a format string.
29 * @param width the number of characters in the result
30 * @param precision the number of fractional digits in the result
31 * @param converter the format conversion specifier
32 * @return suitable for formatting a float or double
33 */
34 static String formatString(final int width, final int precision, final String converter)
35 {
36 return String.format("%%%d.%d%s", width, precision, converter);
37 }
38
39 /**
40 * Format a floating point value.
41 * @param value the value to format
42 * @param width the number of characters in the result
43 * @param precision the number of fractional digits in the result
44 * @return the formatted floating point value
45 */
46 public static String format(final float value, final int width, final int precision)
47 {
48 if (0 == value || Math.abs(value) > 0.01 && Math.abs(value) < 9999.0)
49 {
50 return String.format(formatString(width, precision, "f"), value);
51 }
52 return String.format(formatString(width, precision, "e"), value);
53 }
54
55 /**
56 * Format a floating point value.
57 * @param value the value to format
58 * @param size the number of characters in the result
59 * @return the formatted floating point value
60 */
61 public static String format(final float value, final int size)
62 {
63 return Formatter.format(value, size, Formatter.DEFAULTPRECISION);
64 }
65
66 /**
67 * Format a floating point value.
68 * @param value the value to format
69 * @return the formatted floating point value
70 */
71 public static String format(final float value)
72 {
73 return format(value, Format.DEFAULTSIZE, Formatter.DEFAULTPRECISION);
74 }
75
76 /**
77 * Format a floating point value.
78 * @param value the value to format
79 * @param width the number of characters in the result
80 * @param precision the number of fractional digits in the result
81 * @return the formatted floating point value
82 */
83 public static String format(final double value, final int width, final int precision)
84 {
85 if (0 == value || Math.abs(value) > 0.01 && Math.abs(value) < 9999.0)
86 {
87 return String.format(formatString(width, precision, "f"), value);
88 }
89 return String.format(formatString(width, precision, "e"), value);
90 }
91
92 /**
93 * Format a floating point value.
94 * @param value the value to format
95 * @param size the number of characters in the result
96 * @return the formatted floating point value
97 */
98 public static String format(final double value, final int size)
99 {
100 return format(value, size, Formatter.DEFAULTPRECISION);
101 }
102
103 }