Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
NumberConversions.java
1 package org.bukkit.util;
2 
3 /**
4  * Utils for casting number types to other number types
5  */
6 public final class NumberConversions {
7  private NumberConversions() {}
8 
9  public static int floor(double num) {
10  final int floor = (int) num;
11  return floor == num ? floor : floor - (int) (Double.doubleToRawLongBits(num) >>> 63);
12  }
13 
14  public static int ceil(final double num) {
15  final int floor = (int) num;
16  return floor == num ? floor : floor + (int) (~Double.doubleToRawLongBits(num) >>> 63);
17  }
18 
19  public static int round(double num) {
20  return floor(num + 0.5d);
21  }
22 
23  public static double square(double num) {
24  return num * num;
25  }
26 
27  public static int toInt(Object object) {
28  if (object instanceof Number) {
29  return ((Number) object).intValue();
30  }
31 
32  try {
33  return Integer.valueOf(object.toString());
34  } catch (NumberFormatException e) {
35  } catch (NullPointerException e) {
36  }
37  return 0;
38  }
39 
40  public static float toFloat(Object object) {
41  if (object instanceof Number) {
42  return ((Number) object).floatValue();
43  }
44 
45  try {
46  return Float.valueOf(object.toString());
47  } catch (NumberFormatException e) {
48  } catch (NullPointerException e) {
49  }
50  return 0;
51  }
52 
53  public static double toDouble(Object object) {
54  if (object instanceof Number) {
55  return ((Number) object).doubleValue();
56  }
57 
58  try {
59  return Double.valueOf(object.toString());
60  } catch (NumberFormatException e) {
61  } catch (NullPointerException e) {
62  }
63  return 0;
64  }
65 
66  public static long toLong(Object object) {
67  if (object instanceof Number) {
68  return ((Number) object).longValue();
69  }
70 
71  try {
72  return Long.valueOf(object.toString());
73  } catch (NumberFormatException e) {
74  } catch (NullPointerException e) {
75  }
76  return 0;
77  }
78 
79  public static short toShort(Object object) {
80  if (object instanceof Number) {
81  return ((Number) object).shortValue();
82  }
83 
84  try {
85  return Short.valueOf(object.toString());
86  } catch (NumberFormatException e) {
87  } catch (NullPointerException e) {
88  }
89  return 0;
90  }
91 
92  public static byte toByte(Object object) {
93  if (object instanceof Number) {
94  return ((Number) object).byteValue();
95  }
96 
97  try {
98  return Byte.valueOf(object.toString());
99  } catch (NumberFormatException e) {
100  } catch (NullPointerException e) {
101  }
102  return 0;
103  }
104 }