I am using Cuerdas library in a
ClojureScript program to parse floating point numbers. Sometimes the input from
a user is missing the integral part, like .1 for 0.1. I need to handle this.
But I ran into strange results when comparing the parsed numbers:
(= 0.1 (parse-number "0.1"))
;; => true
(= .1 (parse-number "0.1"))
;; => true
(= 0.1 (parse-number ".1"))
;; => false
(= .1 (parse-number ".1"))
;; => false
The last two results above are surprising to me. When comparing literal number I get the expected result:
(= 0 .0)
;; => true
The strange behavior is also visible when I just parse the decimal number without integer part, like here:
(parse-number ".1")
;; => .1
.1
;; => 0.1
I found out that I can use js/parseFloat to get what I want, but I'm curious
what is the reason for this behavior when using Cuerdas? What is this .1 value
and how is it different than 0.1?