Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Julia, how to format Avogadro's Constant (or other numbers) in engineering notation?

Engineering notation differs from scientific notation in that:

  1. The exponent is always a multiple of 3, and

  2. The digits to the left of the decimal point are scaled to range from 1 to 999.

My use case calls for specifying 0 to 13 digits to the right of the decimal point. The default is 4.

Here are desired examples:

const Avogadro = 6.022140857e23

str = eng_notation(Avogadro, digits=0)             
# str = "602E+21"

str = eng_notation(Avogadro, digits=1)             
# str = "602.2E+21"

# Default 4 digits to right of decimal point.
str = eng_notation(Avogadro)                       
# str = "602.2141E+21"  

str = eng_notation(Avogadro, digits=10)            
# str = "602.2140857000E+21"

# Negative and fractional numbers should also work.
str = eng_notation(-0.01234567, digits=7)          
# str = "-12.4567000E-03"

Any suggestions?

Edit: I updated the requirements to 0 to 13 digits to the right of the decimal point (from 0 to 15 previously).

like image 224
Julia Learner Avatar asked Jan 02 '19 02:01

Julia Learner


1 Answers

Use the NumericIO.jl package

julia> using NumericIO

julia> const Avogadro = 6.022140857e23;

julia> formatted(Avogadro, :ENG, ndigits=4, charset=:ASCII)
"602.2E21"

julia> formatted(Avogadro, :ENG, ndigits=4)
"602.2×10²¹"
like image 77
Przemyslaw Szufel Avatar answered Nov 19 '22 03:11

Przemyslaw Szufel