1 package com.icl.saxon.functions; 2 import com.icl.saxon.*; 3 import com.icl.saxon.expr.*; 4 5 import java.util.*; 6 import java.lang.Math ; 7 import java.text.*; 8 9 10 11 public class Round extends Function { 12 13 14 17 18 public String getName() { 19 return "round"; 20 }; 21 22 26 27 public int getDataType() { 28 return Value.NUMBER; 29 } 30 31 35 36 public Expression simplify() throws XPathException { 37 checkArgumentCount(1, 1); 38 argument[0] = argument[0].simplify(); 39 if (argument[0] instanceof Value) { 40 return evaluate(null); 41 } 42 return this; 43 } 44 45 48 49 public double evaluateAsNumber(Context c) throws XPathException { 50 return round(argument[0].evaluateAsNumber(c)); 51 } 52 53 56 57 public Value evaluate(Context c) throws XPathException { 58 return new NumericValue(evaluateAsNumber(c)); 59 } 60 61 64 65 public int getDependencies() { 66 return argument[0].getDependencies(); 67 } 68 69 72 73 public Expression reduce(int dep, Context c) throws XPathException { 74 Round f = new Round(); 75 f.addArgument(argument[0].reduce(dep, c)); 76 f.setStaticContext(getStaticContext()); 77 return f.simplify(); 78 } 79 80 83 84 public static double round(double arg0) { 85 if (Double.isNaN(arg0)) return arg0; 86 if (Double.isInfinite(arg0)) return arg0; 87 if (arg0==0.0) return arg0; if (arg0 > -0.5 && arg0 < 0.0) return -0.0; 89 if (arg0 > Long.MIN_VALUE && arg0 < Long.MAX_VALUE) { 90 return Math.round(arg0); 91 } 92 double fraction = arg0 % 1.0; 93 if (fraction < 0.5) return arg0 - fraction; 94 return arg0 - fraction + 1.0; 95 96 } 97 98 } 99 100 101 | Popular Tags |