1 2 24 25 package com.lutris.util; 26 27 import java.math.BigDecimal ; 28 29 32 public class Currency extends BigDecimal { 33 36 public Currency() { 37 super(0); 38 } 39 40 44 public Currency(BigDecimal value) { 45 super((value == null) ? 0.0 : value.doubleValue()); 46 } 47 48 51 public Currency(double value) { 52 super(value); 53 } 54 55 58 public Currency(float value) { 59 super((double)value); 60 } 61 62 65 public Currency(String value) { 66 this(Double.valueOf(value).doubleValue()); 67 } 68 69 72 public boolean equals(float value) { 73 return compareTo(new Currency(value)) == 0; 74 } 75 76 79 public boolean equals(double value) { 80 return compareTo(new Currency(value)) == 0; 81 } 82 83 86 public Currency add(Currency val) { 87 return new Currency(super.add(val)); 88 } 89 90 93 public Currency subtract(Currency val) { 94 return new Currency(super.subtract(val)); 95 } 96 97 100 public Currency multiply(Currency val){ 101 return new Currency(super.multiply(val)); 102 } 103 104 107 public Currency multiply(int val){ 108 return multiply(new Currency(val)); 109 } 110 111 114 public Currency divide(Currency val) 115 throws ArithmeticException , IllegalArgumentException { 116 return new Currency(super.divide(val, 2, ROUND_HALF_UP)); 117 } 118 119 122 public Currency divide(int val) 123 throws ArithmeticException , IllegalArgumentException { 124 return divide(new Currency((double)val)); 125 } 126 127 131 public Currency absCurrency() { 132 return (signum() < 0 ? negateCurrency() : this); 133 } 134 135 138 public Currency negateCurrency() { 139 return new Currency(negate()); 140 } 141 142 145 public String toString() { 146 if (scale() == 2) { 147 return super.toString(); 148 } else { 149 return setScale(2, ROUND_HALF_UP).toString(); 150 } 151 } 152 } 153 | Popular Tags |