Keno / SIUnits.jl

Efficient unit-checked computation
Other
70 stars 26 forks source link

Feature request: Pretty printing #76

Open oeb25 opened 8 years ago

oeb25 commented 8 years ago

When printing or converting values with assigned units the output is close to humanly unreadable.

Examples

# current
250Ohm * 30mA
# => 15//2 kg m²s⁻³A⁻¹

2V
# => 2 kg m²s⁻³A⁻¹

# wanted
250Ohm * 30mA
# => 7.5V

2V
# => 2V

If a toString metod like the one in javascript for auto pretty printing on the units isn't posible in Julia, a function like the one below would be just fine

# SIUnit -> String
pretty(5V * 2A)
# => 10W

I know zero to none about Julia, I only use it to calcualte math and physics, so I can unfortunately not implement this myself

ma-laforge commented 8 years ago

Hi @oeb25,

As far as I can tell, there is an ambiguity problem with implementing your requested feature (see https://github.com/Keno/SIUnits.jl/pull/54).

In case you have not had a chance to work it out by now, the following code shows how you can implement your own "pretty" function, while you wait for things to get sorted out:

using SIUnits
using SIUnits.ShortUnits

typealias _Volts{T} SIUnits.SIQuantity{T,2,1,-3,-1,0,0,0,0,0}
typealias _Watts{T} SIUnits.SIQuantity{T,2,1,-3,0,0,0,0,0,0}

pretty(x::_Volts) = "$(x.val)V"
pretty(x::_Watts) = "$(x.val)W"

Comments

typeof(4*W)
# Shows "W" type => SIUnits.SIQuantity{Int64,2,1,-3,0,0,0,0,0,0}