Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia convert number to string in scientific notation

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"

like image 786
cardoza2 Avatar asked Jun 03 '20 16:06

cardoza2


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"
like image 157
Korsbo Avatar answered Nov 13 '22 02:11

Korsbo