Skip to content

Commit

Permalink
Tell Java to do basic mathematics correctly
Browse files Browse the repository at this point in the history
Excluding the math: tag, use BigDecimal for most math tags.
(add/div/sub/mul for now).
  • Loading branch information
mcmonkey4eva committed Oct 18, 2014
1 parent 00bc297 commit b2216a8
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/main/java/net/aufdemrand/denizen/objects/Element.java
@@ -1,5 +1,6 @@
package net.aufdemrand.denizen.objects;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -150,6 +151,14 @@ public Element(String prefix, String string) {
this.element = TagManager.cleanOutputFully(string);
}

private BigDecimal getBD(String text) {
return new BigDecimal(text);
}

public BigDecimal asBigDecimal() {
return getBD(element.replaceAll("%", ""));
}

public double asDouble() {
return Double.valueOf(element.replaceAll("%", ""));
}
Expand Down Expand Up @@ -1483,7 +1492,7 @@ else if (element.toLowerCase().contains(contains.toLowerCase()))
dB.echoError("Element '" + element + "' is not a valid decimal number!");
return Element.NULL.getAttribute(attribute.fulfill(1));
}
return new Element(asDouble() + aH.getDoubleFrom(attribute.getContext(1)))
return new Element(asBigDecimal().add(getBD(attribute.getContext(1))).toString())
.getAttribute(attribute.fulfill(1));
}

Expand All @@ -1500,7 +1509,7 @@ else if (element.toLowerCase().contains(contains.toLowerCase()))
dB.echoError("Element '" + element + "' is not a valid decimal number!");
return Element.NULL.getAttribute(attribute.fulfill(1));
}
return new Element(asDouble() / aH.getDoubleFrom(attribute.getContext(1)))
return new Element(asBigDecimal().divide(getBD(attribute.getContext(1))).toString())
.getAttribute(attribute.fulfill(1));
}

Expand Down Expand Up @@ -1534,7 +1543,7 @@ else if (element.toLowerCase().contains(contains.toLowerCase()))
dB.echoError("Element '" + element + "' is not a valid decimal number!");
return Element.NULL.getAttribute(attribute.fulfill(1));
}
return new Element(asDouble() * aH.getDoubleFrom(attribute.getContext(1)))
return new Element(asBigDecimal().multiply(getBD(attribute.getContext(1))).toString())
.getAttribute(attribute.fulfill(1));
}

Expand All @@ -1551,7 +1560,7 @@ else if (element.toLowerCase().contains(contains.toLowerCase()))
dB.echoError("Element '" + element + "' is not a valid decimal number!");
return Element.NULL.getAttribute(attribute.fulfill(1));
}
return new Element(asDouble() - aH.getDoubleFrom(attribute.getContext(1)))
return new Element(asBigDecimal().subtract(getBD(attribute.getContext(1))).toString())
.getAttribute(attribute.fulfill(1));
}

Expand Down

0 comments on commit b2216a8

Please sign in to comment.