1 package com.github.mygreen.cellformatter.number;
2
3
4
5
6
7
8
9
10 public class FractionNumber extends FormattedNumber {
11
12
13
14
15 private String denominatorPart;
16
17
18
19
20 private String numeratorPart;
21
22
23
24
25 private String wholeNumberPart;
26
27
28
29
30 private boolean wholeType;
31
32 private FractionNumber(final double value) {
33 super(value);
34
35 }
36
37
38
39
40
41
42
43 public static FractionNumber createExactDenominator(final double value, int exactDenom) {
44 return createExactDenominator(value, exactDenom, false);
45 }
46
47
48
49
50
51
52
53
54 public static FractionNumber createExactDenominator(final double value, int exactDenom, boolean wholeType) {
55 final FractionNumberractionNumber.html#FractionNumber">FractionNumber fractionNumber = new FractionNumber(value);
56 fractionNumber.wholeType = wholeType;
57
58 final SimpleFraction fraction = SimpleFraction.createFractionExactDenominator(Math.abs(value), exactDenom);
59 setupFractionPart(fraction, fractionNumber);
60
61 return fractionNumber;
62 }
63
64
65
66
67
68
69
70 public static FractionNumber createMaxDenominator(final double value, int maxDenom) {
71 return createMaxDenominator(value, maxDenom, false);
72 }
73
74
75
76
77
78
79
80
81 public static FractionNumber createMaxDenominator(final double value, int maxDenom, boolean wholeType) {
82 final FractionNumberractionNumber.html#FractionNumber">FractionNumber fractionNumber = new FractionNumber(value);
83 fractionNumber.wholeType = wholeType;
84
85 final SimpleFraction fraction = SimpleFraction.createFractionMaxDenominator(Math.abs(value), maxDenom);
86 setupFractionPart(fraction, fractionNumber);
87
88 return fractionNumber;
89 }
90
91
92
93
94
95
96 private static void setupFractionPart(final SimpleFraction fraction, final FractionNumber fractionNumber) {
97
98 fractionNumber.denominatorPart = String.valueOf(fraction.getDenominator());
99
100 if(fractionNumber.isWholeType()) {
101 int wholeNumber = fraction.getNumerator() / fraction.getDenominator();
102 fractionNumber.wholeNumberPart = wholeNumber == 0 ? "" : String.valueOf(wholeNumber);
103
104 int numerator = fraction.getNumerator() % fraction.getDenominator();
105 fractionNumber.numeratorPart = String.valueOf(numerator);
106
107 } else {
108 fractionNumber.wholeNumberPart = "";
109 fractionNumber.numeratorPart = String.valueOf(fraction.getNumerator());
110
111 }
112 }
113
114
115
116
117
118 public boolean isWholeType() {
119 return wholeType;
120 }
121
122
123
124
125
126 public String getDenominatorPart() {
127 return denominatorPart;
128 }
129
130
131
132
133
134
135 public String getDenominatorPart(final int digit) {
136
137 final int length = denominatorPart.length();
138 if(length < digit || digit <= 0) {
139 return "";
140 }
141
142 return String.valueOf(denominatorPart.charAt(length - digit));
143 }
144
145
146
147
148
149
150 public String getDenominatorPartAfter(final int digit) {
151
152 final int length = denominatorPart.length();
153 if(length < digit || digit <= 0) {
154 return "";
155 }
156
157 return denominatorPart.substring(0, (length - digit + 1));
158 }
159
160
161
162
163
164 public String getNumeratorPart() {
165 return numeratorPart;
166 }
167
168
169
170
171
172
173 public String getNumeratorPart(final int digit) {
174
175 final int length = numeratorPart.length();
176 if(length < digit || digit <= 0) {
177 return "";
178 }
179
180 return String.valueOf(numeratorPart.charAt(length - digit));
181 }
182
183
184
185
186
187
188 public String getNumeratorPartAfter(final int digit) {
189
190 final int length = numeratorPart.length();
191 if(length < digit || digit <= 0) {
192 return "";
193 }
194
195 return numeratorPart.substring(0, (length - digit + 1));
196 }
197
198
199
200
201
202
203 public String getWholeNumberPart() {
204 return wholeNumberPart;
205 }
206
207
208
209
210
211
212 public String getWholeNumberPart(final int digit) {
213
214 final int length = wholeNumberPart.length();
215 if(length < digit || digit <= 0) {
216 return "";
217 }
218
219 return String.valueOf(wholeNumberPart.charAt(length - digit));
220 }
221
222
223
224
225
226
227 public String getWholeNumberPartAfter(final int digit) {
228
229 final int length = wholeNumberPart.length();
230 if(length < digit || digit <= 0) {
231 return "";
232 }
233
234 return wholeNumberPart.substring(0, (length - digit + 1));
235 }
236
237 @Override
238 public String toString() {
239
240 if(isZero()) {
241 return "0";
242 }
243
244 StringBuilder sb = new StringBuilder();
245
246 if(isNegative()) {
247 sb.append("-");
248 }
249
250 if(isWholeType() && !getWholeNumberPart().isEmpty()) {
251 sb.append(getWholeNumberPart()).append(" ");
252 }
253
254 sb.append(getNumeratorPart()).append("/").append(getDenominatorPart());
255
256 return sb.toString();
257
258 }
259
260 }