使用Clojure

时间:2017-05-31 09:53:52

标签: clojure

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

(format "%f" 0.1) ; => 0,1

而不是

(format "%f" 0.1) ; => 0.1

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

1 个答案:

答案 0 :(得分: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"

有什么好处吗?