3
$\begingroup$

I'm in the early stages of learning about units. It seems easy enough to express numerics and symbolics in units, but what I would really like to do is append engineering prefixes (km, mA, MW, etc). For instance, a value of 22000 volts should be represented as 22 kV. A value of 0.005 Amperes should be represented as 5 mA. A value of 0.00255 joules should be represented as 25.5 mJ. Like in the following:

table

Can this be done?

$\endgroup$
3
  • 1
    $\begingroup$ What do you mean by "prefixes"? Of course km, mA, etc. are not prefixes at all. So what do you really want? $\endgroup$ Sep 15, 2017 at 20:35
  • $\begingroup$ Could you add an example of input and expected output? Together with a little background? $\endgroup$
    – Kuba
    Sep 15, 2017 at 20:38
  • $\begingroup$ @Kuba as requested $\endgroup$
    – jamesson
    Sep 15, 2017 at 21:37

3 Answers 3

3
$\begingroup$

Here is a purposefully incomplete solution which I'll leave you to adapt to your needs:

pref = AssociationThread[Delete[Range[-2, 2], 3] -> {"Centi", "Deci", "Deka", "Hecto"}];
pref3 = AssociationThread[Delete[Range[-8, 8], 9] ->
                          {"Yocto", "Zepto", "Atto", "Femto", "Pico", "Nano", "Micro",
                           "Milli", "Kilo", "Mega", "Giga", "Tera", "Peta", "Exa",
                           "Zetta", "Yotta"}];

val = 1.95*^13; (* some sample value *)

{m, e} = {10 #1, #2 - 1} & @@ MantissaExponent[val];
s = Sign[e];
{q, r} = QuotientRemainder[Abs[e], 3];
If[q == 0, {m, Lookup[pref, s r, 1]},
           {m 10^r, Lookup[pref3, s q, 1]}]

which should yield {19.5, "Tera"} in this case.

$\endgroup$
2
  • $\begingroup$ I need to do some reading to understand this, so please don't get mad if I don't accept right away :) $\endgroup$
    – jamesson
    Sep 16, 2017 at 18:11
  • 1
    $\begingroup$ Nobody's rushing you to accept at once, AFAICT. :) Just as long as you accept when you finally decide on what is most satisfactory for you. $\endgroup$ Sep 16, 2017 at 18:13
2
$\begingroup$
UnitConvert[Quantity[8.,"Meters"],"Kilometers"]

0.008 km

$\endgroup$
1
$\begingroup$

What do you mean by engineering "prefixes"?

Is this what you're seeking?

Quantity[8, "Meters"]

8 m

Quantity[10, "Centimeters"]

10 cm

Quantity[20, "Feet"]

20 ft

$\endgroup$
2
  • 1
    $\begingroup$ No, I mean a general interconversion between x*(10^3n) and a unit abbreviation, not only for length. I will change my question to reflect this. $\endgroup$
    – jamesson
    Sep 15, 2017 at 19:04
  • $\begingroup$ And, obviously, put a decimal point where its supposed to go :) $\endgroup$
    – jamesson
    Sep 15, 2017 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.