Posted By

hansamann on 02/02/07


Tagged

numbers groovyseries


Versions (?)


Advertising

Submit Site


Who likes this?

4 people have marked this snippet as a favorite

jsbournival
hendersk
mbanfi
adaptives


Groovy Series: Numbers


Published in: Groovy 



Website Promotion
DIRECTORY
is a crucial factor for all websites that need to gain better organic search engine rankings and increase website traffic.
Submitting your website as part of your Web Promotion strategy to our SEO friendly and high traffic Business Directory for review is an excellent way to gain a valuable backlink and increase your websites visibility online.

Submit Site


Expand | Embed | Plain Text
  1. //the two most common number classes
  2. def intObj = 5
  3. assert intObj.class.name == 'java.lang.Integer'
  4.  
  5. def bigDecimalObj = 5.12345
  6. assert bigDecimalObj.class.name == 'java.math.BigDecimal'
  7.  
  8. //you can explicitly create Long, Float and Double with L / F / D
  9. def longObj = 20L
  10. assert longObj.class.name == 'java.lang.Long'
  11. def floatObj = 40.45F
  12. assert floatObj.class.name == 'java.lang.Float'
  13. def doubleObj = 34.45D
  14. assert doubleObj.class.name == 'java.lang.Double'
  15.  
  16. //if G is used, it checks if the number has a decimal point, then creates a BigInteger or BigDecimal
  17. def bigIntegerObj = 50G
  18. assert bigIntegerObj.class.name == 'java.math.BigInteger'
  19.  
  20. //some coercion stuff
  21. //multiplication, addition and substraction between two floats results in a double
  22. def z = 2F * 2F
  23. assert z.class.name == 'java.lang.Double'
  24.  
  25. //multiplication, addition and substraction with BigInteger or BigDecimal results in BigInteger or BigDecimal
  26. def product = 1.1G * 2 //BigDecimal and Integer
  27. assert product.class.name == 'java.math.BigDecimal'
  28.  
  29. (3G*4).class.name == 'java.math.BigInteger' //BigInteger and Integer
  30.  
  31. //multiplication of two integers
  32. assert (5*4).class.name == 'java.lang.Integer'
  33.  
  34. //multiplication with Long
  35. assert (1L*4).class.name == 'java.lang.Long'
  36.  
  37. //dividing some numbers
  38. (5/5).class.name == 'java.math.BigDecimal'
  39. //if you want an integer, use the intdiv() method
  40. (5.intdiv(5)).class.name == 'java.lang.Integer'
  41. (1.intdiv(5)).class.name == 'java.lang.Integer' //result is 0
  42. assert 1/2 == 0.5
  43.  
  44.  
  45. //some GDK methods for numbers
  46. //think about a java for loop... you don't need it!
  47. def numbers = []
  48. 10.times { numbers << it } //it will contain values from 0 to 9
  49. assert numbers.join(",") == '0,1,2,3,4,5,6,7,8,9'
  50. 10.upto(12) { numbers << it}
  51. assert numbers.join(",") == '0,1,2,3,4,5,6,7,8,9,10,11,12'
  52. 11.downto(10) { numbers << it}
  53. assert numbers.join(",") == '0,1,2,3,4,5,6,7,8,9,10,11,12,11,10'
  54.  
  55. numbers = []
  56. 1.step(2, 0.1) { numbers << it; assert it.class.name == 'java.math.BigDecimal' }
  57. assert numbers.join(",") == '1,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9' //notice 2 is not included
  58.  
  59. numbers = []
  60. 1.0.step(2, 0.1) { numbers << it; assert it.class.name == 'java.math.BigDecimal' }
  61. assert numbers.join(",") == '1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9' //notice how the first value changed to 1.0

Report this snippet 

You need to login to post a comment.