1 package net.sf.saxon.functions; 2 import net.sf.saxon.expr.Token; 3 import net.sf.saxon.expr.XPathContext; 4 import net.sf.saxon.om.Item; 5 import net.sf.saxon.trans.XPathException; 6 import net.sf.saxon.value.AtomicValue; 7 import net.sf.saxon.value.IntegerValue; 8 import net.sf.saxon.value.NumericValue; 9 10 14 15 public final class Rounding extends SystemFunction { 16 17 public static final int FLOOR = 0; 18 public static final int CEILING = 1; 19 public static final int ROUND = 2; 20 public static final int HALF_EVEN = 3; 21 public static final int ABS = 4; 22 23 26 27 public Item evaluateItem(XPathContext context) throws XPathException { 28 29 AtomicValue val0 = (AtomicValue)argument[0].evaluateItem(context); 30 if (val0==null) return null; 31 NumericValue val = (NumericValue)val0.getPrimitiveValue(); 32 33 switch (operation) { 34 case FLOOR: 35 return val.floor(); 36 case CEILING: 37 return val.ceiling(); 38 case ROUND: 39 return val.round(); 40 case HALF_EVEN: 41 int scale = 0; 42 if (argument.length==2) { 43 AtomicValue scaleVal0 = (AtomicValue)argument[1].evaluateItem(context); 44 NumericValue scaleVal = (NumericValue)scaleVal0.getPrimitiveValue(); 45 scale = (int)scaleVal.longValue(); 46 } 47 return val.roundToHalfEven(scale); 48 case ABS: 49 double sign = val.signum(); 50 if (sign < 0) { 51 return val.negate(); 52 } else if (sign == 0) { 53 return val.arithmetic(Token.PLUS, IntegerValue.ZERO, context); 55 } else { 56 return val; 57 } 58 default: 59 throw new UnsupportedOperationException ("Unknown rounding function"); 60 } 61 } 62 63 } 64 65 66 | Popular Tags |