Skip to content

Infrastructure for handling physical units for the Julia programming language

License

Notifications You must be signed in to change notification settings

timholy/Units.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Units.jl

Infrastructure for handling physical units for the Julia programming language

Warning

This Units.jl package is deprecated! Use the Unitful package instead.

Here is some advice on transitioning:

Parsing

You'll need to create your own custom _unit_string_dict and copy-paste the version of parse_quantity below:

_unit_string_dict = ["um" => Micro*Meter, "s" => Second, "us" => Micro*Second, "MHz" => Mega*Hertz]

function parse_quantity(s::String, strict::Bool = true)
    # Find the last character of the numeric component
    m = match(r"[0-9\.\+-](?![0-9\.\+-])", s)
    if m == nothing
        error("String does not have a 'value unit' structure")
    end
    val = float64(s[1:m.offset])
    ustr = strip(s[m.offset+1:end])
    if isempty(ustr)
        if strict
            error("String does not have a 'value unit' structure")
        else
            return val
        end
    end
    val * _unit_string_dict[ustr]
end

Representation

Define a NonSIUnit quantity for each type you want to represent in fixed terms as follows:

Micron = SIUnits.NonSIUnit{typeof(Meter),:µm}()
convert(::Type{SIUnits.SIQuantity},::typeof(Micron)) = Micro*Meter

Then display objects and arrays like this: as(x, Micron). If you need this as a string, you can use an IOBuffer.

About

Infrastructure for handling physical units for the Julia programming language

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages