Permalink
Browse files

add Integer#decimals

  • Loading branch information...
bakkdoor committed Jun 4, 2012
1 parent 595f2f4 commit d9f276c7e889f631fdec246c3050679361ab3e61
Showing with 30 additions and 0 deletions.
  1. +21 −0 lib/integer.fy
  2. +9 −0 tests/integer.fy
View
@@ -50,4 +50,25 @@ class Integer {
return value
}
}
+
+ def decimals {
+ """
+ @return @Array@ of all decimals of @self.
+
+ Returns all decimals of an Integer as an Array.
+
+ Example:
+ 100 decimals # => [1, 0, 0]
+ 12345 decimals # => [1, 2, 3, 4, 5]
+ """
+
+ decimals = []
+ tmp = self
+ while: { tmp >= 10 } do: {
+ decimals << (tmp modulo: 10)
+ tmp = tmp div: 10
+ }
+ decimals << tmp
+ decimals reverse
+ }
}
View
@@ -0,0 +1,9 @@
+FancySpec describe: Integer with: {
+ it: "returns its decimals as an array" with: 'decimals when: {
+ (0..9) each: |i| { i decimals is: [i] }
+ 10 decimals is: [1, 0]
+ 100 decimals is: [1, 0, 0]
+ 123 decimals is: [1, 2, 3]
+ 998811 decimals is: [9, 9, 8, 8, 1, 1]
+ }
+}

0 comments on commit d9f276c

Please sign in to comment.