Clojure舍入为小数位 [英] Clojure rounding to decimal places
本文介绍了Clojure舍入为小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有字符串表示十进制值,
ex:0.010,0.010000000.01000
I have strings which represents the decimal values, ex: "0.010", "0.0100000" "00.01000"
格式,
ex:#。##
I want to round them to specified format, ex: #.##
在Java中,我们有:
In Java we have:
public BigDecimal setScale(int newScale, RoundingMode roundingMode) {
return setScale(newScale, roundingMode.oldMode);
}
在Clojure中实现这个最好的方法是什么? / p>
What is the best approach to achieve this in Clojure rather than using interop?
推荐答案
您可以使用Clojure的格式
它应该为您的问题提供解决方案。以下是一些示例和参考:
You can use Clojure's format
for this purpose. It should provide you a solution to your problem. Here are some examples and a reference:
user=> (format "%.2f" 0.010)
"0.01"
user=> (format "%.2f" 0.010000)
"0.01"
user=> (format "%.2f" 00.010000000)
user=> (doc format)
-------------------------
clojure.core/format
([fmt & args])
Formats a string using java.lang.String.format, see java.util.Formatter for format
string syntax
这篇关于Clojure舍入为小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文