-
Notifications
You must be signed in to change notification settings - Fork 20
/
NumberFormat.java
189 lines (170 loc) · 7.04 KB
/
NumberFormat.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.kernel.commons;
import sirius.kernel.nls.NLS;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.math.RoundingMode;
import java.text.DecimalFormatSymbols;
/**
* Used to define the parameters used to create a string representation of a number.
* <p>
* Provides a set of default formats and also describes the parameters used to format a number.
* This is used by {@link Amount} to create string representations.
*/
public class NumberFormat {
/**
* Describes the default format used to create string representations of percentages.
* <p>
* It therefore specifies two decimal places which are rounded {@link RoundingMode#HALF_UP}.
* It uses the decimal format symbols for the currently active language provided by
* {@link sirius.kernel.nls.NLS}. A percent sign <tt>%</tt> is used as suffix
*
* @see sirius.kernel.nls.NLS#getDecimalFormatSymbols()
*/
public static final NumberFormat PERCENT = new NumberFormat(2, RoundingMode.HALF_UP, null, true, "%");
/**
* Describes a format which rounds to two decimal places.
* <p>
* It specifies {@link RoundingMode#HALF_UP} as rounding mode and uses
* the decimal format symbols for the currently active language provided by
* {@link sirius.kernel.nls.NLS}.
*
* @see sirius.kernel.nls.NLS#getDecimalFormatSymbols()
*/
public static final NumberFormat TWO_DECIMAL_PLACES = new NumberFormat(2, RoundingMode.HALF_UP, null, true, null);
/**
* Describes a format which rounds to two decimal places.
* <p>
* It specifies {@link RoundingMode#HALF_UP} as rounding mode and uses
* the decimal format symbols for machine formats, provided by
* {@link sirius.kernel.nls.NLS}.
*
* @see sirius.kernel.nls.NLS#getMachineFormatSymbols()
*/
public static final NumberFormat MACHINE_TWO_DECIMAL_PLACES =
new NumberFormat(2, RoundingMode.HALF_UP, NLS.getMachineFormatSymbols(), false, null);
/**
* Describes a format which rounds to three decimal places.
* <p>
* It specifies {@link RoundingMode#HALF_UP} as rounding mode and uses
* the decimal format symbols for machine formats, provided by
* {@link sirius.kernel.nls.NLS}.
*
* @see sirius.kernel.nls.NLS#getMachineFormatSymbols()
*/
public static final NumberFormat MACHINE_THREE_DECIMAL_PLACES =
new NumberFormat(3, RoundingMode.HALF_UP, NLS.getMachineFormatSymbols(), false, null);
/**
* Describes a format which rounds to up to five decimal places.
* <p>
* It specifies {@link RoundingMode#HALF_UP} as rounding mode and uses
* the decimal format symbols for machine formats, provided by
* {@link sirius.kernel.nls.NLS}.
* <p>
* This method is intended to return a machine representation without loosing any data while formatting. As most
* probably five decimal places are too much to output, this should be used in conjunction with
* {@link Amount#toSmartRoundedString(NumberFormat)}.
*
* @see sirius.kernel.nls.NLS#getMachineFormatSymbols()
*/
public static final NumberFormat MACHINE_FIVE_DECIMAL_PLACES =
new NumberFormat(5, RoundingMode.HALF_UP, NLS.getMachineFormatSymbols(), false, null);
/**
* Describes a format which rounds to integer numbers (no decimal places).
* <p>
* It specifies {@link RoundingMode#HALF_UP} as rounding mode and uses
* the decimal format symbols for the currently active language provided by
* {@link sirius.kernel.nls.NLS}.
*
* @see sirius.kernel.nls.NLS#getDecimalFormatSymbols()
*/
public static final NumberFormat NO_DECIMAL_PLACES = new NumberFormat(0, RoundingMode.HALF_UP, null, true, null);
/**
* Describes a format which rounds to integer numbers (no decimal places).
* <p>
* It specifies {@link RoundingMode#HALF_UP} as rounding mode and uses
* the decimal format symbols for machine formats, provided by
* {@link sirius.kernel.nls.NLS}.
*
* @see sirius.kernel.nls.NLS#getMachineFormatSymbols()
*/
public static final NumberFormat MACHINE_NO_DECIMAL_PLACES =
new NumberFormat(0, RoundingMode.HALF_UP, NLS.getMachineFormatSymbols(), false, null);
private final String suffix;
private final int scale;
private final boolean useGrouping;
private final RoundingMode roundingMode;
private final DecimalFormatSymbols formatSymbols;
/**
* Creates a new number format used to format {@link Amount amounts}.
*
* @param scale contains the number of decimal places shown. Use {@link Amount#toSmartRoundedString(NumberFormat)}
* to remove unwanted zeros.
* @param roundingMode contains the rounding mode to use. Most commonly {@link RoundingMode#HALF_UP} will be
* correct.
* @param formatSymbols contains the {@link DecimalFormatSymbols} to use. This parameter can be <tt>null</tt> to
* use the format symbols of the current language, which is present, when this format is used.
* @param useGrouping determines if grouping (by thousands) is used.
* @param suffix the suffix to append to a formatted string
*/
public NumberFormat(int scale,
@Nonnull RoundingMode roundingMode,
@Nullable DecimalFormatSymbols formatSymbols,
boolean useGrouping,
@Nullable String suffix) {
this.scale = scale;
this.roundingMode = roundingMode;
this.formatSymbols = formatSymbols;
this.useGrouping = useGrouping;
this.suffix = suffix;
}
/**
* Returns the suffix appended to a formatted string, like a % sign.
*
* @return the suffix used by this format
*/
public String getSuffix() {
return suffix;
}
/**
* Returns the desired number of decimal places.
*
* @return the number of decimal places used by this format
*/
public int getScale() {
return scale;
}
/**
* Determines the rounding mode if more decimal places are available.
*
* @return the rounding mode used by this format
*/
public RoundingMode getRoundingMode() {
return roundingMode;
}
/**
* Returns the utilized format symbols when creating a string representation.
*
* @return the decimal format symbols used by this format
*/
public DecimalFormatSymbols getDecimalFormatSymbols() {
if (formatSymbols == null) {
return NLS.getDecimalFormatSymbols();
}
return formatSymbols;
}
/**
* Determines if grouping separators (by thousands) should be placed or not.
*
* @return <tt>true</tt> if grouping separators are used, <tt>false</tt> otherwise
*/
public boolean isUseGrouping() {
return useGrouping;
}
}