Skip to content

How can I get the list of units accepted by mathjs? #3178

Answered by josdejong
AFZL210 asked this question in Q&A
Discussion options

You must be logged in to vote

You can lookup math.Unit.UNITS, and write out every unit with every prefix that it can have:

const allUnits = Object.values(math.Unit.UNITS).flatMap(unit => {
  return Object.values(unit.prefixes).map(prefix => prefix.name + unit.name)
})
// [ "meter", "decameter", "hectometer", ... ] 

This currently returns a list with 2748 items.

If you just need to know if some string is a unit, you can use the following method:

const isUnit = math.Unit.isValuelessUnit("millimeter") // true

Replies: 2 comments 4 replies

Comment options

dvd101x
Mar 21, 2024
Collaborator Sponsor

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@AFZL210
Comment options

@orelbn
Comment options

@josdejong
Comment options

@orelbn
Comment options

Answer selected by AFZL210
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants