58 lines
1.7 KiB
Java
58 lines
1.7 KiB
Java
package sum;
|
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigInteger;
|
|
|
|
/**
|
|
* @author Nikita Doschennikov (me@fymio.us)
|
|
*/
|
|
public class SumBigDecimalHex {
|
|
public static void main(String[] args) {
|
|
BigDecimal res = new BigDecimal("0");
|
|
for (String arg : args) {
|
|
StringBuilder builder = new StringBuilder();
|
|
for (char c : arg.toCharArray()) {
|
|
if (!Character.isWhitespace(c)) {
|
|
builder.append(c);
|
|
} else {
|
|
res = res.add(compute(builder.toString()));
|
|
|
|
builder = new StringBuilder();
|
|
}
|
|
}
|
|
res = res.add(compute(builder.toString()));
|
|
}
|
|
System.out.println(res);
|
|
}
|
|
|
|
static BigDecimal compute(String num) {
|
|
BigDecimal res = BigDecimal.ZERO;
|
|
|
|
if (num == null || num.isEmpty()) {
|
|
return res;
|
|
} else if (num.startsWith("0x") || num.startsWith("0X")) {
|
|
num = num.toLowerCase();
|
|
num = num.substring(2);
|
|
if (num.contains("s")) {
|
|
|
|
int sIndex = num.indexOf('s');
|
|
|
|
String mantissaHex = num.substring(0, sIndex);
|
|
String exponentHex = num.substring(sIndex + 1);
|
|
|
|
BigInteger mantissa = new BigInteger(mantissaHex, 16);
|
|
BigInteger exponent = new BigInteger(exponentHex, 16);
|
|
|
|
res = res.add(new BigDecimal(mantissa).scaleByPowerOfTen(exponent.negate().intValueExact()));
|
|
} else {
|
|
res = res.add(new BigDecimal(new BigInteger(num, 16)));
|
|
}
|
|
} else {
|
|
res = new BigDecimal(num);
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
}
|