8

In Java, I can do the following to format a floating point number for display:

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

In theory, I should be able to do the same thing with this Clojure:

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

However, when I run the above Clojure snippet in the REPL, I get the following exception:

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

What am I doing wrong?

1 Answer 1

15

Java's String.format takes an Object[] (or Object...), to use String.format in Clojure you need to wrap your arguments in an array:

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

Clojure provides a wrapper for format that's easier to use:

(format "%2f" 5.0)

Kyle

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.