View Javadoc
1   package org.opentrafficsim.core.value;
2   
3   /**
4    * Format a floating point number in a reasonable way. <br>
5    * I've experienced problems with the %g conversions that caused array bounds violations. Those versions of the JRE that do
6    * <b>not</b> throw such Exceptions use one digit less than specified in the %g conversions. <br >
7    * TODO check how to always format numbers corresponding to the Locale used.
8    * <p>
9    * This file was generated by the OpenTrafficSim value classes generator, 09 mrt, 2015
10   * <p>
11   * Copyright (c) 2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
13   * <p>
14   * @version 09 mrt, 2015 <br>
15   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
17   */
18  public final class Format
19  {
20      /**
21       * This class shall never be instantiated.
22       */
23      private Format()
24      {
25          // Prevent instantiation of this class
26      }
27  
28      /** Default total width of formatted value. */
29      public static final int DEFAULTSIZE = 9;
30  
31      /** Default number of fraction digits. */
32      public static final int DEFAULTPRECISION = 3;
33  
34      /**
35       * Build a format string.
36       * @param width int; the number of characters in the result
37       * @param precision int; the number of fractional digits in the result
38       * @param converter String; the format conversion specifier
39       * @return String; suitable for formatting a float or double
40       */
41      private static String formatString(final int width, final int precision, final String converter)
42      {
43          return String.format("%%%d.%d%s", width, precision, converter);
44      }
45  
46      /**
47       * Format a floating point value.
48       * @param value float; the value to format
49       * @param width int; the number of characters in the result
50       * @param precision int; the number of fractional digits in the result
51       * @return String; the formatted floating point value
52       */
53      public static String format(final float value, final int width, final int precision)
54      {
55          if (0 == value || Math.abs(value) > 0.01 && Math.abs(value) < 9999.0)
56          {
57              return String.format(formatString(width, precision, "f"), value);
58          }
59          return String.format(formatString(width, precision, "e"), value);
60      }
61  
62      /**
63       * Format a floating point value.
64       * @param value float; the value to format
65       * @param size int; the number of characters in the result
66       * @return String; the formatted floating point value
67       */
68      public static String format(final float value, final int size)
69      {
70          return Format.format(value, size, Format.DEFAULTPRECISION);
71      }
72  
73      /**
74       * Format a floating point value.
75       * @param value float; the value to format
76       * @return String; the formatted floating point value
77       */
78      public static String format(final float value)
79      {
80          return format(value, Format.DEFAULTSIZE, Format.DEFAULTPRECISION);
81      }
82  
83      /**
84       * Format a floating point value.
85       * @param value double; the value to format
86       * @param width int; the number of characters in the result
87       * @param precision int; the number of fractional digits in the result
88       * @return String; the formatted floating point value
89       */
90      public static String format(final double value, final int width, final int precision)
91      {
92          if (0 == value || Math.abs(value) > 0.01 && Math.abs(value) < 9999.0)
93          {
94              return String.format(formatString(width, precision, "f"), value);
95          }
96          return String.format(formatString(width, precision, "e"), value);
97      }
98  
99      /**
100      * Format a floating point value.
101      * @param value double; the value to format
102      * @param size int; the number of characters in the result
103      * @return String; the formatted floating point value
104      */
105     public static String format(final double value, final int size)
106     {
107         return Format.format(value, size, Format.DEFAULTPRECISION);
108     }
109 
110     /**
111      * Format a floating point value.
112      * @param value double; the value to format
113      * @return String; the formatted floating point value
114      */
115     public static String format(final double value)
116     {
117         return format(value, Format.DEFAULTSIZE, Format.DEFAULTPRECISION);
118     }
119 
120 }