clojure - 如何在 Clojure 中惯用地打印货币?

标签 clojure currency number-formatting

我正在使用以下内容:

(defn dollars [input] (str "$" (format "%.2f" input)))

(不使用逗号)

但我认为一定有一种使用 pprint/cl-format 的方法。

我的问题是:如何在 Clojure 中惯用地打印货币?

最佳答案

如果您需要更多的资金处理工作,而不仅仅是格式化它,您可以考虑使用 https://github.com/clojurewerkz/money (请参阅格式化部分),它包装joda-money。这不仅涵盖格式,还涵盖舍入等其他常见问题。

user=> (mf/format (ma/amount-of mc/USD 10000))
"$10,000.00"
user=> (mf/format (ma/amount-of mc/USD 10000) java.util.Locale/GERMANY)
"USD10.000,00"

编辑

您可以传递amount-of 舍入方式。例如

user=> (mf/format (ma/amount-of mc/USD 10.111111111111111))

ArithmeticException Scale of amount 10.11111111111111 is greater than the scale of the currency USD  org.joda.money.Money.of (Money.java:74)

user=> (mf/format (ma/amount-of mc/USD 10.111111111111111 (java.math.RoundingMode/HALF_DOWN)))
"$10.11"

关于clojure - 如何在 Clojure 中惯用地打印货币?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28515214/

相关文章:

android - 哪些语言在Android中开发原生应用比较成熟[Java除外]

angular - 如何在Angular中向右显示货币符号

android - 简单的 EditText 货币格式化程序

java - Java 中的货币精度(不是 BigDecimal)

Clojure-为什么这段代码不能在 clojure 中工作,我是否缺少一些惰性求值陷阱?

macros - 如何让Clojure的 `while`返回一个值

map - Clojure 宏中关键字的奇怪行为

java - 字符串转 double Java错误

ios - 使用 numberFromString 生成 NSDecimalNumber 时如何获得没有分数错误的舍入值?

java - 格式化数字字符串