1 package com.github.mygreen.cellformatter.number;
2
3 import java.math.RoundingMode;
4 import java.text.DecimalFormat;
5
6
7
8
9
10
11
12
13 public class PercentNumber extends DecimalNumber {
14
15 public PercentNumber(final double value, final int decimalScale, final int permilles) {
16 super(value, decimalScale, permilles);
17 }
18
19 public PercentNumber(final double value, final int decimalScale) {
20 this(value, decimalScale, 0);
21 }
22
23 @Override
24 protected void init() {
25
26 if(isZero()) {
27 this.integerPart = "";
28 this.decimalPart = "";
29 return;
30 }
31
32 final StringBuilder sb = new StringBuilder();
33 sb.append("#");
34 if(getScale() > 0) {
35 sb.append(".");
36 }
37
38
39 for(int i=0; i < getScale(); i++) {
40 sb.append("#");
41 }
42
43
44 sb.append("%");
45
46 final DecimalFormat format = new DecimalFormat(sb.toString());
47 format.setRoundingMode(RoundingMode.HALF_UP);
48
49 double num = Math.abs(getValue());
50 if(getPermilles() > 0) {
51 num /= Math.pow(1000, getPermilles());
52 }
53
54 String str = format.format(num);
55
56
57 str = str.substring(0, str.length()-1);
58
59
60 setupIntegerAndDecimalPart(str);
61
62 }
63
64 @Override
65 public String toString() {
66 return super.toString() + "%";
67 }
68
69
70 }