Julia convert number to string in scientific notation

Viewed 1459

Does Julia have a way to convert a number to scientific notation in the string type?

For instance:

300 becomes "3.00E+02" or 0.0045 becomes "4.5E-03" or 3.14159 becomes "3.14159E+00"

1 Answers

You can use the standard library Printf for this.

julia> using Printf
julia> @printf "%.2E" 300
3.00E+02

if you don't want to print, but rather convert to an appropriately formatted string, you can do

julia> using Printf
julia> str = @sprintf "%.2E" 300
"3.00E+02"
Related