|
| 1 | +package com.codingapi.springboot.framework.math; |
| 2 | + |
| 3 | + |
| 4 | +import java.math.BigDecimal; |
| 5 | +import java.math.BigInteger; |
| 6 | +import java.math.RoundingMode; |
| 7 | + |
| 8 | +public class Arithmetic { |
| 9 | + |
| 10 | + private BigDecimal value; |
| 11 | + |
| 12 | + private final static int DEFAULT_SCALE = 4; |
| 13 | + private final static RoundingMode DEFAULT_ROUNDING_MODE = RoundingMode.HALF_UP; |
| 14 | + |
| 15 | + public static Arithmetic zero(){ |
| 16 | + return parse(0); |
| 17 | + } |
| 18 | + |
| 19 | + public static Arithmetic one(){ |
| 20 | + return parse(1); |
| 21 | + } |
| 22 | + |
| 23 | + public static Arithmetic parse(int value){ |
| 24 | + return new Arithmetic(value); |
| 25 | + } |
| 26 | + |
| 27 | + public static Arithmetic parse(float value){ |
| 28 | + return new Arithmetic(value); |
| 29 | + } |
| 30 | + |
| 31 | + public static Arithmetic parse(double value){ |
| 32 | + return new Arithmetic(value); |
| 33 | + } |
| 34 | + |
| 35 | + public static Arithmetic parse(long value){ |
| 36 | + return new Arithmetic(value); |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + public Arithmetic(String value) { |
| 41 | + this.value = new BigDecimal(value); |
| 42 | + } |
| 43 | + |
| 44 | + public Arithmetic(int value) { |
| 45 | + this(String.valueOf(value)); |
| 46 | + } |
| 47 | + |
| 48 | + public Arithmetic(float value) { |
| 49 | + this(String.valueOf(value)); |
| 50 | + } |
| 51 | + |
| 52 | + public Arithmetic(double value) { |
| 53 | + this(String.valueOf(value)); |
| 54 | + } |
| 55 | + |
| 56 | + public Arithmetic(long value) { |
| 57 | + this(String.valueOf(value)); |
| 58 | + } |
| 59 | + |
| 60 | + public Arithmetic add(Arithmetic val){ |
| 61 | + this.value = this.value.add(val.value); |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + public Arithmetic add(String val){ |
| 67 | + this.value = this.value.add(new Arithmetic(val).value); |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + public Arithmetic add(int val){ |
| 72 | + this.value = this.value.add(new Arithmetic(val).value); |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + public Arithmetic add(float val){ |
| 77 | + this.value = this.value.add(new Arithmetic(val).value); |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + public Arithmetic add(double val){ |
| 82 | + this.value = this.value.add(new Arithmetic(val).value); |
| 83 | + return this; |
| 84 | + } |
| 85 | + |
| 86 | + public Arithmetic add(long val){ |
| 87 | + this.value = this.value.add(new Arithmetic(val).value); |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + public Arithmetic sub(Arithmetic val){ |
| 92 | + this.value =this.value.subtract(val.value); |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + public Arithmetic sub(String val){ |
| 97 | + this.value =this.value.subtract(new Arithmetic(val).value); |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + public Arithmetic sub(int val){ |
| 102 | + this.value =this.value.subtract(new Arithmetic(val).value); |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + public Arithmetic sub(float val){ |
| 107 | + this.value =this.value.subtract(new Arithmetic(val).value); |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + public Arithmetic sub(double val){ |
| 113 | + this.value =this.value.subtract(new Arithmetic(val).value); |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + public Arithmetic sub(long val){ |
| 119 | + this.value =this.value.subtract(new Arithmetic(val).value); |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + public Arithmetic mul(Arithmetic val){ |
| 125 | + this.value = this.value.multiply(val.value); |
| 126 | + return this; |
| 127 | + } |
| 128 | + |
| 129 | + public Arithmetic mul(String val){ |
| 130 | + this.value = this.value.multiply(new Arithmetic(val).value); |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
| 134 | + public Arithmetic mul(int val){ |
| 135 | + this.value = this.value.multiply(new Arithmetic(val).value); |
| 136 | + return this; |
| 137 | + } |
| 138 | + |
| 139 | + public Arithmetic mul(float val){ |
| 140 | + this.value = this.value.multiply(new Arithmetic(val).value); |
| 141 | + return this; |
| 142 | + } |
| 143 | + |
| 144 | + public Arithmetic mul(double val){ |
| 145 | + this.value = this.value.multiply(new Arithmetic(val).value); |
| 146 | + return this; |
| 147 | + } |
| 148 | + |
| 149 | + public Arithmetic mul(long val){ |
| 150 | + this.value = this.value.multiply(new Arithmetic(val).value); |
| 151 | + return this; |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + public Arithmetic div(Arithmetic val){ |
| 156 | + this.value = this.value.divide(val.value,DEFAULT_SCALE, DEFAULT_ROUNDING_MODE); |
| 157 | + return this; |
| 158 | + } |
| 159 | + |
| 160 | + public Arithmetic div(String val){ |
| 161 | + this.value = this.value.divide(new Arithmetic(val).value, DEFAULT_SCALE,DEFAULT_ROUNDING_MODE); |
| 162 | + return this; |
| 163 | + } |
| 164 | + |
| 165 | + public Arithmetic div(int val){ |
| 166 | + this.value = this.value.divide(new Arithmetic(val).value,DEFAULT_SCALE, DEFAULT_ROUNDING_MODE); |
| 167 | + return this; |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | + public Arithmetic div(float val){ |
| 172 | + this.value = this.value.divide(new Arithmetic(val).value,DEFAULT_SCALE, DEFAULT_ROUNDING_MODE); |
| 173 | + return this; |
| 174 | + } |
| 175 | + |
| 176 | + public Arithmetic div(double val){ |
| 177 | + this.value = this.value.divide(new Arithmetic(val).value,DEFAULT_SCALE,DEFAULT_ROUNDING_MODE); |
| 178 | + return this; |
| 179 | + } |
| 180 | + |
| 181 | + public Arithmetic div(long val){ |
| 182 | + this.value = this.value.divide(new Arithmetic(val).value, DEFAULT_SCALE,DEFAULT_ROUNDING_MODE); |
| 183 | + return this; |
| 184 | + } |
| 185 | + |
| 186 | + public BigDecimal getValue() { |
| 187 | + return value; |
| 188 | + } |
| 189 | + |
| 190 | + public String getStringValue() { |
| 191 | + return value.toString(); |
| 192 | + } |
| 193 | + |
| 194 | + public BigInteger getBigIntegerValue() { |
| 195 | + return value.toBigInteger(); |
| 196 | + } |
| 197 | + |
| 198 | + public int getIntValue() { |
| 199 | + return value.intValue(); |
| 200 | + } |
| 201 | + |
| 202 | + public double getDoubleValue() { |
| 203 | + return value.doubleValue(); |
| 204 | + } |
| 205 | + |
| 206 | + public float getFloatValue() { |
| 207 | + return value.floatValue(); |
| 208 | + } |
| 209 | + |
| 210 | + public long getLongValue() { |
| 211 | + return value.longValue(); |
| 212 | + } |
| 213 | + |
| 214 | + public Arithmetic halfUpScale2(){ |
| 215 | + return halfUpScale(2); |
| 216 | + } |
| 217 | + |
| 218 | + public Arithmetic halfUpScale(int scale){ |
| 219 | + value = value.setScale(scale,RoundingMode.HALF_UP); |
| 220 | + return this; |
| 221 | + } |
| 222 | + |
| 223 | +} |
| 224 | + |
0 commit comments