Percentage formatting in Clojure

This seems to be obvious, but how do you convert the Clojure relation type to percent?

(format "%3s" (/ 1 2))
;; "1/2"

(format "%3f" (/ 1 2))
;; Throws an error
+4
source share
3 answers

You can always convert to Float:

(format "%3f" (float (/ 1 2)))
+6
source

cl-format, which is derived from Common Lisp format, is more flexible than Java-based Clojure formatin some situations:

(require 'clojure.pprint)
(clojure.pprint/cl-format nil "~f" (/ 1 2)) ;=> "0.5"
+5
source

Must read source:

(.decimalValue (/ 1 2))

does what i need.

+4
source

Source: https://habr.com/ru/post/1599692/


All Articles