1 package gnu.ecmascript; 2 3 public class Convert 4 { 5 public static double toNumber(Object x) 6 { 7 if (x instanceof java.lang.Number ) 8 return ((java.lang.Number )x).doubleValue(); 9 if (x instanceof Boolean ) 12 return ((Boolean )x).booleanValue() ? 1 : 0; 13 if (x instanceof String ) 14 { 15 try 16 { 17 return Double.valueOf((String )x).doubleValue(); 19 } 20 catch (NumberFormatException ex) 21 { 22 return Double.NaN; 23 } 24 } 25 return Double.NaN; 27 } 28 29 public static double toInteger(double x) 30 { 31 if (Double.isNaN(x)) 32 return 0.0; 33 return x < 0.0 ? Math.ceil (x) : Math.floor (x); 34 } 35 36 public static double toInteger(Object x) 37 { 38 return toInteger(toNumber(x)); 39 } 40 41 public int toInt32 (double x) 42 { 43 if (Double.isNaN(x) || Double.isInfinite(x)) 44 return 0; 45 return (int) x; 47 } 48 49 public int toInt32 (Object x) 50 { 51 return toInt32(toNumber(x)); 52 } 53 } 54 | Popular Tags |