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.

  • 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
  • 2
    $\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
  • $\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


Your Answer

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

Browse other questions tagged or ask your own question.