Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add Integer#decimals

  • Loading branch information...
commit d9f276c7e889f631fdec246c3050679361ab3e61 1 parent 595f2f4
@bakkdoor authored
Showing with 30 additions and 0 deletions.
  1. +21 −0 lib/integer.fy
  2. +9 −0 tests/integer.fy
View
21 lib/integer.fy
@@ -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
9 tests/integer.fy
@@ -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]
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.