1
$\begingroup$

Consider some calculation yielding Quantity[x,"B"] as output ("B" interpreted as byte).

  1. Is there a way to automatically convert this to the unit "GB" or "MB" or "TB" etc as the case may be. i.e. instead of checking y in x<2^(10 y), is there some inbuilt way of doing this?

Consider now the quantity Quantity[x,"s"]. ("s" interpreted as second).

Depending on x, the human readable unit may be micro seconds to days. This again has the same solution structure as the file-size case mentioned earlier. Only the base unit is different.

  1. Say one implements the explicit solution mentioned in 1. If one wants to modify the function to now accommodate time based Quantitys too, one needs to know whether the input arg is of "data" category or "time" category. Are there meta categories for units in Mathematica? e.g. "KB,MB,GB,TB"->data, "s,h,year,days"->"time"

I did try CommonUnits, but it didn't work.

$\endgroup$
5
  • 2
    $\begingroup$ Have you already seen this? $\endgroup$ Jan 8, 2021 at 9:47
  • $\begingroup$ @J.M.'sennui no I hadn't, I have now....in essence its the same as what I mentioned as an explicit solution in 1. So gather there are no inbuilt functions? $\endgroup$
    – lineage
    Jan 8, 2021 at 9:52
  • $\begingroup$ Not that I recall, no. $\endgroup$ Jan 8, 2021 at 11:07
  • 3
    $\begingroup$ In the Wolfram Function Repository there are resource functions BytesToQuantity, SecondsToQuantity, and MetriNumberString that might be relevant here. $\endgroup$ Jan 8, 2021 at 14:46
  • 1
    $\begingroup$ Per 2: You can make UnitConvert canonicalize units for you and work from there (see the docs) which gives you the "meta-category" $\endgroup$
    – b3m2a1
    Jan 8, 2021 at 17:53

1 Answer 1

1
$\begingroup$

This question is very much subject to taste. Here is a few ideas that may be tailored to the users' tastes. I happened to use time units for my example.

v1=Quantity[1,"Exaseconds"]
v2=Quantity[1,"Millenia"]
v3=Quantity[0.123000456,"seconds"];
unitList={"Eons","Megayears","Millenia","Years","Months","Days","Hours","Seconds","Milliseconds","Microseconds","Nanoseconds"}
human[quantity_Quantity,units_List]:= Block[
{result},
result=UnitConvert[quantity,MixedUnit[units]];
result=Evaluate[Select[{result[[1,1]],result[[2,1]]}\[Transpose],#[[1]]!=0&]\[Transpose]];
Quantity[MixedMagnitude[result[[1]]],MixedUnit[result[[2]]]]
]
human[v1,unitList]
human[v2,unitList]
human[v3,unitList]
approximate[quantity_Quantity,units_List]:= Block[
{result},
result=UnitConvert[quantity,MixedUnit[units]];
result=Evaluate[First[Select[{result[[1,1]],result[[2,1]]}\[Transpose],#[[1]]!=0&]]\[Transpose]];
Quantity@@result
]
approximate[v1,unitList]
approximate[v2,unitList]
approximate[v3,unitList]
1Es
1millennium
{Eons,Megayears,Millenia,Years,Months,Days,Hours,Seconds,Milliseconds,Microseconds,Nanoseconds}
31\[ThinSpace]eons709\[ThinSpace]Myr791\[ThinSpace]millennia458\[ThinSpace]yr2\[ThinSpace]mo25\[ThinSpace]days17\[ThinSpace]h2800\[ThinSpace]s
1millennium
123\[ThinSpace]ms456.\[ThinSpace]ns
31eons
1millennium
123ms

Here is a potentially useful utility function:

makeUnits = Function[base, (StringJoin[#1, base] & ) /@ 
    {"quetta", "ronna", "yotta", "zetta", "exa", "peta", 
     "tera", "giga", "mega", "kilo", "", "milli", "micro", 
     "nano", "pico", "femto", "atto", "zepto", "yocto", 
     "ronto", "quecto"}]

{quettajoule,ronnajoule,yottajoule,zettajoule,exajoule,petajoule,terajoule,gigajoule,megajoule,kilojoule,joule,millijoule,microjoule,nanojoule,picojoule,femtojoule,attojoule,zeptojoule,yoctojoule,rontojoule,quectojoule}

human[Quantity[1,"eV"],unitList]
160\[ThinSpace]zJ217\[ThinSpace]yJ663\[ThinSpace]rJ400\[ThinSpace]qJ
human[Quantity[1,"kWh"],unitList]
3\[ThinSpace]MJ600\[ThinSpace]kJ
human[Quantity[1,"BTU"],unitList]
1\[ThinSpace]kJ55\[ThinSpace]J55\[ThinSpace]mJ852\[ThinSpace]\[Mu]J620\[ThinSpace]nJ
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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