public static Decimal zero() { try { return new Decimal("0"); } catch (Exception e) { return null; // won't happen } }
public Decimal copy() { Decimal result = new Decimal(); result.precision = precision; result.scientific = scientific; result.negative = negative; result.digits = digits; result.decimal = decimal; return result; }
public static Decimal one() { try { return new Decimal("1"); } catch (Exception e) { return null; // won't happen } }
private void testCompares(String v1, String v2, int outcome) throws UcumException { Decimal d1 = new Decimal(v1); Decimal d2 = new Decimal(v2); int result = d1.comparesTo(d2); check(result == outcome, "Compare fail: "+v1+".compares("+v2+") should be "+Integer.toString(outcome)+" but was "+Integer.toString(result)); }
public void multiplyValue(int multiplicand) { value = value.multiply(new Decimal(multiplicand)); }
public void divideValue(int divisor) throws UcumException { value = value.divide(new Decimal(divisor)); }
private void testInteger(int i) throws UcumException { Decimal d = new Decimal(i); check(d.asInteger() == i, "Failed to round trip the integer "+Integer.toString(i)); }
private Value parseValue(XmlPullParser xpp, String context) throws XmlPullParserException, UcumException, IOException { checkAtElement(xpp, "value", context); Decimal val = null; if (xpp.getAttributeValue(null, "value") != null) try { if (xpp.getAttributeValue(null, "value").contains(".")) val = new Decimal(xpp.getAttributeValue(null, "value"), 24); // unlimited precision for these else val = new Decimal(xpp.getAttributeValue(null, "value")); } catch (NumberFormatException e) { throw new XmlPullParserException("Error reading "+context+": "+e.getMessage()); } Value value = new Value(xpp.getAttributeValue(null, "Unit"), xpp.getAttributeValue(null, "UNIT"), val); value.setText(readElement(xpp, "value", context, true)); return value; }
private void testString(String s, String st, String std) throws UcumException { Decimal dec = new Decimal(s); String s1 = dec.toString(); String s2 = dec.asScientific(); check(s1.equals(st), "decimal: expected "+st+" but got "+s1); check(s2.equals(std), "scientific: expected "+std+" but got "+s2); dec = new Decimal(std); s1 = dec.asDecimal(); check(s1.equals(st), "decimal(2): expected "+st+" but got "+s1); }
private void testTrunc(String s1, String s2) throws UcumException { Decimal o1 = new Decimal(s1); Decimal o2 = o1.trunc(); check(o2.asDecimal().equals(s2), "wrong trunc - expected "+s2+" but got "+o2.asDecimal()); }
public int asInteger() throws UcumException { if (!isWholeNumber()) throw new UcumException("Unable to represent "+toString()+" as an integer"); if (comparesTo(new Decimal(Integer.MIN_VALUE)) < 0) throw new UcumException("Unable to represent "+toString()+" as a signed 8 byte integer"); if (comparesTo(new Decimal(Integer.MAX_VALUE)) > 0) throw new UcumException("Unable to represent "+toString()+" as a signed 8 byte integer"); return Integer.parseInt(asDecimal()); }
private Prefix parsePrefix(XmlPullParser xpp) throws XmlPullParserException, IOException, UcumException { Prefix prefix = new Prefix(xpp.getAttributeValue(null, "Code"), xpp.getAttributeValue(null, "CODE")); xpp.next(); skipWhitespace(xpp); prefix.getNames().add(readElement(xpp, "name", "prefix "+prefix.getCode(), false)); prefix.setPrintSymbol(readElement(xpp, "printSymbol", "prefix "+prefix.getCode(), false)); checkAtElement(xpp, "value", "prefix "+prefix.getCode()); prefix.setValue(new Decimal(xpp.getAttributeValue(null, "value"), 24)); readElement(xpp, "value", "prefix "+prefix.getCode(), true); xpp.next(); skipWhitespace(xpp); return prefix; }
private void init() { register(new CelsiusHandler()); register(new FahrenheitHandler()); register(new HoldingHandler("[p'diop]", "deg")); register(new HoldingHandler("%[slope]", "deg")); register(new HoldingHandler("[hp_X]", "1")); register(new HoldingHandler("[hp_C]", "1")); register(new HoldingHandler("[pH]", "mol/l")); register(new HoldingHandler("Np", "1")); register(new HoldingHandler("B", "1")); register(new HoldingHandler("B[SPL]", "10*-5.Pa", new Decimal(2))); register(new HoldingHandler("B[V]", "V")); register(new HoldingHandler("B[mV]", "mV")); register(new HoldingHandler("B[uV]", "uV")); register(new HoldingHandler("B[W]", "W")); register(new HoldingHandler("B[kW]", "kW")); register(new HoldingHandler("bit_s", "1")); }