2012-02-18 76 views
8

在Java中,我可以做以下格式化浮点数显示:为什么我的字符串格式在Clojure中失败?

String output = String.format("%2f" 5.0); 
System.out.println(output); 

从理论上讲,我应该可以做同样的事情与此Clojure的:

(let [output (String/format "%2f" 5.0)] 
    (println output)) 

然而,当我在REPL中运行上面的Clojure代码段时,出现以下异常:

java.lang.Double cannot be cast to [Ljava.lang.Object; 
[Thrown class java.lang.ClassCastException 

我在做什么错?

回答

15

Java的String.format需要一个Object[](或Object...),使用String.format Clojure中你需要用你的论点中的数组:

(String/format "%2f" (into-array [5.0])) 

的Clojure提供了封装的格式更容易使用:

(format "%2f" 5.0) 

Kyle

相关问题