Skip to content

Commit

Permalink
Simplify the str-to-decimal function
Browse files Browse the repository at this point in the history
  • Loading branch information
CryptoPascal31 committed Oct 16, 2023
1 parent caec86a commit 011b236
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions pact/contracts/util-strings.pact
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,10 @@
(let* ((is-negative (= "-" (take 1 in)))
(in (if is-negative (drop 1 in) in))
(parts (split "." in)))
(enforce (or? (= 1) (= 2) (length parts)) "Invalid format")
(let* ((int-part (at 0 parts))
(has-decimal (= 2 (length parts)))
(dec-part (if has-decimal (at 1 parts) "0"))
(precision (if has-decimal (length dec-part) 0))
(enforce (or? (is-singleton) (is-pair) parts) "Invalid format")
(let* ((int-part (first parts))
(dec-part (if (is-pair parts) (last parts) "0"))
(precision (if (is-pair parts) (length dec-part) 0))
(dec-multiplier (^ 0.1 (dec precision)))
(str-to-dint (lambda (x) (dec (str-to-int 10 x))))
(val (+ (str-to-dint int-part) (* dec-multiplier (str-to-dint dec-part)))))
Expand Down

0 comments on commit 011b236

Please sign in to comment.