Writing a float to an IOStream in decimal notation


#1

Is there a way to force the decimal notation for floats for printing or writing in a file?


#2

@sprintf can do that:

julia> @sprintf("%.3f", Float64(π))
"3.142"

julia> @sprintf("%.8f", Float64(π))
"3.14159265"

julia> @sprintf("%.8f", 3e8)
"300000000.00000000"

#3

Or, if you want to print to a stream directly, then @printf:

julia> @printf(STDOUT, "%f", π)
3.141593

#4

I often use @printf to do such printing.


#5

Thanks @rdeits @sunoru, it did the trick.