Formatting float string representation to match english with Clojure
The locale on my machine is sv_SE.utf8, I want the format to follow the en_GB.utf8 conventions instead of string formatting. Currently
(format "%f" 0.1) ; => 0,1
instead
(format "%f" 0.1) ; => 0.1
It looks like I cannot pass the language for formatting. Is there any other way to get around this problem? I still want to use the format because of its other capabilities.
+3
source to share
1 answer
This works for me (my default laptop standard is set to fr-FR
, France!):
(import java.util.Locale)
; => java.util.Locale
(defn my-format [fmt n & [locale]]
(let [locale (if locale (Locale. locale)
(Locale/getDefault))]
(String/format locale fmt (into-array Object [n]))))
; => #'dev/my-format
(my-format "%f" 0.1)
; => "0,100000"
(my-format "%f" 0.1 "en-GB")
; => "0.100000"
Any good?
+5
source to share