Increasingly Functional.
by Joshua Miller | on Twitter | on the web | on github

Transit, ClojureScript, and BigDecimal

November 12th 2015

Tagged: clojure

Tonight while updating the Harrisburg Crime Tracker to use Transit (more on how shared data representations help later), I ran into a small issue I didn't find a whole lot of documentation on and wasn't immediately obvious to me at first, so I thought I'd share the solution.

I'm storing latitudes and longitudes in Postgres (using the DECIMAL type), which become Java BigDecimals. These are encoded in the Transit format as "arbitrary precision decimal" with the ~f tag.

If you're not paying attention, you might assume you'll just get a JavaScript float on the ClojureScript side, but obviously given JavaScript's issues with floating-point arithmetic, on second thought you realize that sort of defeats the purpose. Instead, what transit-cljs provides is a com.cognitect.BigDecimal.

So how to turn that into a normal float?

(.-rep big-decimal-val)