1 16 17 package org.apache.velocity.tools.generic; 18 19 46 public class MathTool 47 { 48 55 public Number add(Object num1, Object num2) 56 { 57 Number n1 = toNumber(num1); 58 Number n2 = toNumber(num2); 59 if (n1 == null || n2 == null) 60 { 61 return null; 62 } 63 double value = n1.doubleValue() + n2.doubleValue(); 64 return matchType(n1, n2, value); 65 } 66 67 68 75 public Number sub(Object num1, Object num2) 76 { 77 Number n1 = toNumber(num1); 78 Number n2 = toNumber(num2); 79 if (n1 == null || n2 == null) 80 { 81 return null; 82 } 83 double value = n1.doubleValue() - n2.doubleValue(); 84 return matchType(n1, n2, value); 85 } 86 87 88 95 public Number mul(Object num1, Object num2) 96 { 97 Number n1 = toNumber(num1); 98 Number n2 = toNumber(num2); 99 if (n1 == null || n2 == null) 100 { 101 return null; 102 } 103 double value = n1.doubleValue() * n2.doubleValue(); 104 return matchType(n1, n2, value); 105 } 106 107 108 115 public Number div(Object num1, Object num2) 116 { 117 Number n1 = toNumber(num1); 118 Number n2 = toNumber(num2); 119 if (n1 == null || n2 == null || n2.doubleValue() == 0.0) 120 { 121 return null; 122 } 123 double value = n1.doubleValue() / n2.doubleValue(); 124 return matchType(n1, n2, value); 125 } 126 127 128 135 public Number pow(Object num1, Object num2) 136 { 137 Number n1 = toNumber(num1); 138 Number n2 = toNumber(num2); 139 if (n1 == null || n2 == null) 140 { 141 return null; 142 } 143 double value = Math.pow(n1.doubleValue(), n2.doubleValue()); 144 return matchType(n1, n2, value); 145 } 146 147 148 155 public Number max(Object num1, Object num2) 156 { 157 Number n1 = toNumber(num1); 158 Number n2 = toNumber(num2); 159 if (n1 == null || n2 == null) 160 { 161 return null; 162 } 163 double value = Math.max(n1.doubleValue(), n2.doubleValue()); 164 return matchType(n1, n2, value); 165 } 166 167 168 175 public Number min(Object num1, Object num2) 176 { 177 Number n1 = toNumber(num1); 178 Number n2 = toNumber(num2); 179 if (n1 == null || n2 == null) 180 { 181 return null; 182 } 183 double value = Math.min(n1.doubleValue(), n2.doubleValue()); 184 return matchType(n1, n2, value); 185 } 186 187 188 194 public Number abs(Object num) 195 { 196 Number n = toNumber(num); 197 if (n == null) 198 { 199 return null; 200 } 201 double value = Math.abs(n.doubleValue()); 202 return matchType(n, value); 203 } 204 205 206 211 public Integer ceil(Object num) 212 { 213 Number n = toNumber(num); 214 if (n == null) 215 { 216 return null; 217 } 218 return new Integer ((int)Math.ceil(n.doubleValue())); 219 } 220 221 222 226 public Integer floor(Object num) 227 { 228 Number n = toNumber(num); 229 if (n == null) 230 { 231 return null; 232 } 233 return new Integer ((int)Math.floor(n.doubleValue())); 234 } 235 236 237 245 public Integer round(Object num) 246 { 247 Number n = toNumber(num); 248 if (n == null) 249 { 250 return null; 251 } 252 return new Integer ((int)Math.rint(n.doubleValue())); 253 } 254 255 256 269 public Double roundTo(Object decimals, Object num) 270 { 271 Number i = toNumber(decimals); 272 Number d = toNumber(num); 273 if (i == null || d == null) 274 { 275 return null; 276 } 277 int places = i.intValue(); 279 double value = d.doubleValue(); 280 if (places == 0) 281 { 282 value = (int)(value + .5); 283 } 284 else 285 { 286 double shift = Math.pow(10, places); 287 value = value * shift; 288 value = (int)(value + .5); 289 value = value / shift; 290 } 291 return new Double (value); 292 } 293 294 295 305 public Integer roundToInt(Object num) 306 { 307 return round(num); 308 } 309 310 311 316 public Double getRandom() 317 { 318 return new Double (Math.random()); 319 } 320 321 322 338 public Number random(Object num1, Object num2) 339 { 340 Number n1 = toNumber(num1); 341 Number n2 = toNumber(num2); 342 if (n1 == null || n2 == null) 343 { 344 return null; 345 } 346 347 double diff = n2.doubleValue() - n1.doubleValue(); 348 double random = (diff * Math.random()) + n1.doubleValue(); 352 353 String in = n1.toString() + n2.toString(); 355 if (in.indexOf('.') < 0) 356 { 357 return matchType(n1, n2, Math.floor(random)); 359 } 360 return new Double (random); 363 } 364 365 366 368 377 public Integer toInteger(Object num) 378 { 379 Number n = toNumber(num); 380 if (n == null) 381 { 382 return null; 383 } 384 return new Integer (n.intValue()); 385 } 386 387 388 397 public Double toDouble(Object num) 398 { 399 Number n = toNumber(num); 400 if (n == null) 401 { 402 return null; 403 } 404 return new Double (n.intValue()); 405 } 406 407 408 419 public Number toNumber(Object num) 420 { 421 if (num == null) 422 { 423 return null; 424 } 425 if (num instanceof Number ) 426 { 427 return (Number )num; 428 } 429 try 430 { 431 return parseNumber(String.valueOf(num)); 432 } 433 catch (NumberFormatException nfe) 434 { 435 return null; 436 } 437 } 438 439 440 442 445 protected Number matchType(Number in, double out) 446 { 447 return matchType(in, null, out); 448 } 449 450 451 459 protected Number matchType(Number in1, Number in2, double out) 460 { 461 467 boolean isWhole = (Math.rint(out) == out); 469 470 if (isWhole) 471 { 472 String in = in1.toString(); 475 isWhole = (in.indexOf('.') < 0); 476 477 if (isWhole && in2 != null) 479 { 480 in = in2.toString(); 481 isWhole = (in.indexOf('.') < 0); 482 } 483 } 484 485 if (!isWhole) 486 { 487 return new Double (out); 488 } 489 else if (out > Integer.MAX_VALUE || out < Integer.MIN_VALUE) 490 { 491 return new Long ((long)out); 492 } 493 else 494 { 495 return new Integer ((int)out); 496 } 497 } 498 499 500 509 protected Number parseNumber(String value) throws NumberFormatException 510 { 511 if (value.indexOf('.') < 0) 513 { 514 long i = new Long (value).longValue(); 516 if (i > Integer.MAX_VALUE || i < Integer.MIN_VALUE) 517 { 518 return new Long (i); 519 } 520 else 521 { 522 return new Integer ((int)i); 523 } 524 } 525 else 526 { 527 return new Double (value); 528 } 529 } 530 531 } 532 | Popular Tags |