Replies: 4 comments
-
Unfortunately, to the best of my knowledge, the Unit implementation currently in use makes no more connection between 'g' and 'gram' than that they are units of the same "dimension" (namely mass) that have the same value with respect to the fundamental unit of mass (namely, 0.001 -- the fundamental unit of mass in Unit is the kilogram). It does know internally that 'grams' is an alias of 'gram', but as far as I can tell, it does not expose this information in any property or method you can reach from the math object. (There has been talk of splitting out Unit into a separate project. In fact, that separate project exists at https://github.com/ericman314/UnitMath but nobody has done the work to make sure it could be incorporated into mathjs and to do so if possible. However, I don't know if it would improve your situation as far as canonicalizing units -- you could take a look.) So at the moment within mathjs, about the best I can recommend is for you to go through the roughly 250 keys of Perhaps @josdejong or @ericman314 has other thoughts. Sorry there doesn't seem to be a better method at the moment. @er |
Beta Was this translation helpful? Give feedback.
-
Ah, the lengthy discussion about splitting Unit off is #2739. |
Beta Was this translation helpful? Give feedback.
-
I believe you should be able to do this in https://github.com/ericman314/UnitMath. We made a big effort to make the output from the API more predictable, which means if you have it configured right, @gwhitney is right that we still have more work ahead of us to integrate the library back into math.js. Unfortunately I don't have as much spare time as I used to, but, if a few of us made a concerted effort I'm sure we could get it done. |
Beta Was this translation helpful? Give feedback.
-
@gwhitney @ericman314 Thank you for the feedback. Unfortunately, identifying units and amounts is my primary use case and the reason I reached for mathjs. UnitMath looks promising, but for now I've opted instead for my own regex-based approach. I appreciate all of the effort! Hopefully between mathjs and UnitMath this will be relatively simple in the future. |
Beta Was this translation helpful? Give feedback.
-
Hello! I'm using mathjs to parse unit information from user input and I have a question about unit aliases.
In my application, a user will input some quantity string, and I'd like to parse this input and save amount and unit information if it can be extracted.
This works well enough, except that I get several different representations of the same unit (e.g. "grams", "gram", "g", etc.). I'd like to use consistent unit names, but I'm not sure how to derive, for example,
"grams"
frommath.unit("10g")
ormath.unit("1 gram")
. Is there some method to convert units from one alias to another?Ideally, my function would convert values like so:
"5 grams"
→{ amount: 5, unit: "grams" }
"1 gram"
→{ amount: 1, unit: "grams" }
"100 g
→{ amount: 100, unit: "grams" }
"2 ounces"
→{ amount: 1, unit: "ounces" }
"1 ounce"
→{ amount: 1, unit: "ounces" }
"1 oz"
→{ amount: 1, unit: "ounces" }
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions