Numbers are stored internally in a binary format. Clojure prints them
using formatting that's determined by the output stream. It defaults
to using scientific notation for some ranges of floating point
numbers. If you want more control about how the numbers are output,
you have some options including the clojure.core functions format and
printf, and clojure.contrib.pprint functions such as and cl-format
pprint.
Here's an example using format:
user=> (doc format)
-------------------------
clojure.core/format
([fmt & args])
Formats a string using java.lang.String.format, see
java.util.Formatter for format
string syntax
nil
user=> (format "%s %f %.4f %.8f" 8.3e-6 8.3e-6 8.3e-6 8.3e-6)
"8.3E-6 0.000008 0.0000 0.00000830"
user=>
--Steve
On Jul 16, 2009, at 10:13 AM, flodel wrote:
I have a Clojure script that reads numbers in a normal format like
0.0000012, but after processing, returns in a scientific format, for
example, 8.03E-6
Is there a way to turn this scientific notation off?
[...]Clojure prints them using formatting that's determined by the output stream[...]
> what do you mean ? For what I know, the output streams have nothing
> to do with formatting ?
You're right, Laurent, thanks for the correction.
REPL output is printed using prn which (for Doubles) ultimately calls
".toString" on the Double object. It's there that scientific notation
is used for some numbers based on their magnitude:
user=> (.toString 0.1)
"0.1"
user=> (.toString 0.01)
"0.01"
user=> (.toString 0.0001)
"1.0E-4"
user=> (class 0.01)
java.lang.Double
user=>
--Steve