5
$\begingroup$

For example, I want to convert 179 centimeters into 5 feet and 10.4724 inches instead of 70.4724 inches (as this website did). Maybe in the future I would need to convert days to {years, months, days} combination, degrees to {degrees, minutes, seconds}, etc. I can indeed write some codes to perform the task, but after all personal codes are full of flaws and lack of exception handling, so I am looking for a built-in function.

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. $\endgroup$ – bbgodfrey Jan 20 '15 at 23:57
  • 1
    $\begingroup$ In WolframAlpha (or Mathematica after "==") just type "179 centimeters in English units". There are built-in functions for time in Mathematica, however, such as FromDate[], all well documented. $\endgroup$ – David G. Stork Jan 21 '15 at 0:06
  • $\begingroup$ @DavidG.Stork Yes, $DateList[{y,m,d,h,m,s}]$ can solve some of the problems concerning time, but it has many restrictions: {y,m,d} cannot be 0; cannot display 3.14 days, but 3 days 3 hours 21 minutes 36 seconds instead. $\endgroup$ – Franklin Yu Jan 21 '15 at 0:27
  • $\begingroup$ Could you please give one very specific example of the kind of problem you want solved? The conversion of days to {years, months, days} is straightforward. What else? $\endgroup$ – David G. Stork Jan 21 '15 at 0:29
  • $\begingroup$ @DavidG.Stork it is interesting that these are called English units. I wondered what W|A would tell me for "32 hogsheads + 9 pints + 17 gills in French units". It replied that this is $2.062\times10^{11}$ cubic Frenches. You learn something new every day. Oh, and it also thinks that this is a measure of engine displacement, although the French gauge system is used to describe the diameter of catheters. $\endgroup$ – Oleksandr R. Jan 21 '15 at 0:33
12
$\begingroup$
UnitConvert[Quantity[179., "Centimeters"], MixedRadix["Feet", "Inches"]]

returns

Quantity[MixedRadix[5, 10.472440944881885`], MixedRadix["Feet", "Inches"]]

which formats as 5'10.4724"

$\endgroup$
  • $\begingroup$ +1 It is a very nice undocumented feature! $\endgroup$ – ybeltukov Jan 21 '15 at 2:34
  • $\begingroup$ @ybeltukov I actually didn't know this was undocumented; searching in the documentation center gets no results (!!!) but I'm sure I read this in the documentation center somewhere. $\endgroup$ – 2012rcampion Jan 21 '15 at 2:37
  • $\begingroup$ Is there an undocumented Tag? This is not the first example. Reasonable to expect WRI at some point may mine this site. $\endgroup$ – alancalvitti Jan 21 '15 at 4:21
  • $\begingroup$ @alancalvitti undocumented tag added. $\endgroup$ – Franklin Yu Jan 21 '15 at 4:22
  • 1
    $\begingroup$ @george2079 UnitConvert[Quantity["Meter"], MixedRadix["Feet", "Inches", "Inches"]] becomes 3'3"47/127". Not power-of-two fractions though. $\endgroup$ – 2012rcampion Jan 23 '15 at 23:40
3
$\begingroup$

There is no such build-in function. However, one can write it easily

convert[q_Quantity, units_List] := MapAt[{Total@#} &, #, -1][[2 ;;, 1]] &@
  FoldList[Through@{IntegerPart, FractionalPart}[Last@#/#2] #2 &,
   {q, q}, Reverse@Sort[Quantity /@ units]]

convert[Quantity[179., "Centimeters"], {"Feets", "Inches"}]
(* {Quantity[5, "Feet"], Quantity[10.4724, "Inches"]} *)

convert[Quantity[1.2345, "Meter"], {"Millimeters", "Centimeters"}]
(*{Quantity[123, "Centimeters"], Quantity[4.5, "Millimeters"]}*)

convert[Quantity[1.2345, "Degree"], {"Degree", "ArcMinute", "ArcSecond"}]
(*{Quantity[1, "AngularDegrees"], Quantity[14, "ArcMinutes"], Quantity[4.2, "ArcSeconds"]}*)
$\endgroup$
2
$\begingroup$

10.4 introduces MixedMagnitude and MixedUnit, eg:

Quantity[MixedMagnitude[{1/2, 2, 3}], 
 MixedUnit[{"Hours", "Minutes", "Seconds"}]]

which btw, renders with bogus DisplayForm

0 h 32 min 3 s

And try all 0's.

$\endgroup$

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.