Infrastructure for handling physical units for the Julia programming language
Clone or download
timholy Merge pull request #1 from dpsanders/patch-1
Link to SIUnits package repo in README.
Latest commit 03aef35 Nov 3, 2015
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Deprecate Units.jl in favor of SIUnits.jl Feb 16, 2014
test Add REQUIRE, runtests.jl Oct 16, 2013
.gitignore Initial commit Mar 4, 2013
LICENSE.txt Initial commit Mar 4, 2013
README.md Link to SIUnits package repo in README. Nov 3, 2015
REQUIRE Freeze all development (make uninstallable for julia 0.3-) Aug 14, 2014

README.md

Units.jl

Infrastructure for handling physical units for the Julia programming language

Warning

This Units.jl package is deprecated! Use the SIUnits 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.