Skip to content

Commit

Permalink
Merge pull request #4 from ajtucker/master
Browse files Browse the repository at this point in the history
Number parsing
  • Loading branch information
lkitching authored Feb 23, 2018
2 parents 96c7ed6 + b2f6253 commit eaf9f92
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/grafter/extra/cell/string.clj
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@

(defmethod parseValue java.lang.String [x]
(let [cleaned (strip-non-value-chars x)]
(if (= "" cleaned)
nil
(if (.contains cleaned ".")
(Double/parseDouble cleaned)
(Integer/parseInt cleaned)))))
(try
(Integer/parseInt cleaned)
(catch NumberFormatException e
(try
(BigInteger. cleaned)
(catch NumberFormatException e
(try
(BigDecimal. cleaned)
(catch NumberFormatException e
nil))))))))

(defmethod parseValue :default [x]
x)
6 changes: 5 additions & 1 deletion test/grafter/extra/cell/string_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
(is (= (parseValue "123")
123))
(is (= (parseValue "1.23")
1.23))
1.23M))
(is (= (parseValue "2256672315")
2256672315N))
(is (= (parseValue "123123123123123123123123123123123.123123131231231231231231231231231312312312313123")
123123123123123123123123123123123.123123131231231231231231231231231312312312313123M))
(is (= (parseValue \a)
nil))
(is (= (parseValue "abc")
Expand Down

0 comments on commit eaf9f92

Please sign in to comment.