@Override public String toString() { return asDecimal(); }
public boolean isWholeNumber() { return !asDecimal().contains("."); }
public String compose(Canonical can, boolean value) { StringBuilder b = new StringBuilder(); if (value) b.append(can.getValue().asDecimal()); boolean first = true; for (CanonicalUnit c : can.getUnits()) { if (first) first = false; else b.append("."); b.append(c.getBase().getCode()); if (c.getExponent() != 1) b.append(c.getExponent()); } return b.toString(); }
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 List<Base> opDivideBy(List<Base> left, List<Base> right) throws PathEngineException { if (left.size() == 0) throw new PathEngineException("Error performing /: left operand has no value"); if (left.size() > 1) throw new PathEngineException("Error performing /: left operand has more than one value"); if (!left.get(0).isPrimitive()) throw new PathEngineException(String.format("Error performing -: left operand has the wrong type (%s)", left.get(0).fhirType())); if (right.size() == 0) throw new PathEngineException("Error performing /: right operand has no value"); if (right.size() > 1) throw new PathEngineException("Error performing /: right operand has more than one value"); if (!right.get(0).isPrimitive()) throw new PathEngineException(String.format("Error performing /: right operand has the wrong type (%s)", right.get(0).fhirType())); List<Base> result = new ArrayList<Base>(); Base l = left.get(0); Base r = right.get(0); if (l.hasType("integer", "decimal") && r.hasType("integer", "decimal")) { Decimal d1; try { d1 = new Decimal(l.primitiveValue()); Decimal d2 = new Decimal(r.primitiveValue()); result.add(new DecimalType(d1.divide(d2).asDecimal())); } catch (UcumException e) { throw new PathEngineException(e); } } else throw new PathEngineException(String.format("Error performing /: left and right operand have incompatible or illegal types (%s, %s)", left.get(0).fhirType(), right.get(0).fhirType())); return result; }
private List<Base> opDivideBy(List<Base> left, List<Base> right) throws PathEngineException { if (left.size() == 0) throw new PathEngineException("Error performing /: left operand has no value"); if (left.size() > 1) throw new PathEngineException("Error performing /: left operand has more than one value"); if (!left.get(0).isPrimitive()) throw new PathEngineException(String.format("Error performing -: left operand has the wrong type (%s)", left.get(0).fhirType())); if (right.size() == 0) throw new PathEngineException("Error performing /: right operand has no value"); if (right.size() > 1) throw new PathEngineException("Error performing /: right operand has more than one value"); if (!right.get(0).isPrimitive()) throw new PathEngineException(String.format("Error performing /: right operand has the wrong type (%s)", right.get(0).fhirType())); List<Base> result = new ArrayList<Base>(); Base l = left.get(0); Base r = right.get(0); if (l.hasType("integer", "decimal") && r.hasType("integer", "decimal")) { Decimal d1; try { d1 = new Decimal(l.primitiveValue()); Decimal d2 = new Decimal(r.primitiveValue()); result.add(new DecimalType(d1.divide(d2).asDecimal())); } catch (UcumException e) { throw new PathEngineException(e); } } else throw new PathEngineException(String.format("Error performing /: left and right operand have incompatible or illegal types (%s, %s)", left.get(0).fhirType(), right.get(0).fhirType())); return result; }
d1 = new Decimal(l.primitiveValue()); Decimal d2 = new Decimal(r.primitiveValue()); result.add(new DecimalType(d1.modulo(d2).asDecimal())); } catch (UcumException e) { throw new PathEngineException(e);
d1 = new Decimal(l.primitiveValue()); Decimal d2 = new Decimal(r.primitiveValue()); result.add(new IntegerType(d1.divInt(d2).asDecimal())); } catch (UcumException e) { throw new PathEngineException(e);
d1 = new Decimal(l.primitiveValue()); Decimal d2 = new Decimal(r.primitiveValue()); result.add(new IntegerType(d1.divInt(d2).asDecimal())); } catch (UcumException e) { throw new PathEngineException(e);
d1 = new Decimal(l.primitiveValue()); Decimal d2 = new Decimal(r.primitiveValue()); result.add(new DecimalType(d1.modulo(d2).asDecimal())); } catch (UcumException e) { throw new PathEngineException(e);
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 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); }
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()); }