Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Pretty printing #76

Open
oeb25 opened this issue Oct 22, 2015 · 1 comment
Open

Feature request: Pretty printing #76

oeb25 opened this issue Oct 22, 2015 · 1 comment

Comments

@oeb25
Copy link

oeb25 commented Oct 22, 2015

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
Copy link

Hi @oeb25,

As far as I can tell, there is an ambiguity problem with implementing your requested feature
(see #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

  • The _Volts & _Watts typealias-es are defined simply for convenience/readability.... and underscores were added to avoid collisions with other symbols out there.
  • To figure out what "SIQuantity" you need for a unit, run the following (replace with unit of interest):
typeof(4*W)
# Shows "W" type => SIUnits.SIQuantity{Int64,2,1,-3,0,0,0,0,0,0}

This was referenced Dec 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants