使用Clojure根据英语语言环境格式化float的字符串表示形式

Rov*_*ion 6 clojure

我的机器上的语言环境是sv_SE.utf8,我希望格式化时遵循en_GB.utf8的约定,而不是格式化字符串.目前

(format "%f" 0.1) ; => 0,1
Run Code Online (Sandbox Code Playgroud)

代替

(format "%f" 0.1) ; => 0.1
Run Code Online (Sandbox Code Playgroud)

好像我无法通过语言环境来格式化.有没有其他办法解决这个问题?由于其他功能,我仍然希望继续使用格式.

Sco*_*ott 9

这对我有用(我的笔记本电脑默认语言环境设置为fr-FR,法国!):

(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"
Run Code Online (Sandbox Code Playgroud)

好不好?