I have been struggling with for hours now, but cannot understand the unit implementation.
What I want to achieve is to convert any unit object to a unit object which is
I thought there will be an easy method on the unit object to achieve this, but I cannot find one.
Also, another related question is: it seems that Unit.prototype.toString() converts to some "best fitting" prefixes. However, what "best fit" means is not clear to me, for example 1000 m is NOT displayed as 1 km, (but stays 1000 m); however 1001 m is displayed as 1.001 km. This seems awkward.
Thanks for your question. I'm not exactly sure what you mean with your points 1 and 2, maybe you can give an example? There is a function math.to to convert to another unit, but I guess you've seen that function.
I'm sure we can fine tune the way units are stringified. It may make more sense to render 999 m, 1 km, 1.001 km instead. See these unit tests to get an idea of how "best fitting prefixes" are currently implemented.
I made the mistake of asking too many questions at the same time, it is confusing.
So focus on just one.
Let's assume I do this (stupid example, but shows the point):
t = 10^(-23) minute; d = 23 cm; v= d / t
then v will be
Now I want to have that expressed in base SI units, i.e. create a unit which is expressed in m/s. Of course, in a concrete example where I know that this is a velocity, and I know that the base unit for that is m/s, I can convert to m/s easily. However, if the whole thing comes from a user input, I do not know what unit to convert to.
I've been trying to understand the unit implementation. I see that the dimensions (in this case something like LENGTH, TIME: [+1, -1] are stored internally (not showing other zero dimensions). What I do not find is how to get the base (valueless) unit for that dimension in SI that I want to convert to.
So, what I want to do is, let's say, create a function that takes a unit as input and returns another unit which is the same value expressed in base SI units.
I've submitted PR #817 to address this. I've been wanting a "convert to SI" function for some time!
Great! I'll close this issue.
Most helpful comment
I've submitted PR #817 to address this. I've been wanting a "convert to SI" function for some time!