Replies: 6 comments 2 replies
-
|
You're absolutely right – the behavior is inconsistent and can be confusing. If you want consistent units, regardless of how small or large the value is, you can explicitly convert to the desired prefix using .to() before formatting: This way, both values are expressed using the same unit (mW), and you avoid unexpected automatic prefix changes. |
Beta Was this translation helpful? Give feedback.
-
|
I think saying that the behavior of .format() is "inconsistent" is a bit strong; it's totally accurate, however, to say that the behavior is not fully and clearly documented. The fact is that the current behavior of .format() deliberately includes a bias toward the "current unit", and it prefers tenths over hundreds. You can demonstrate these rules in the parser window at mathjs.org: The behavior is entirely consistent to these rules. The rules may not conform to your judgment of the "best" unit, and they do not have the property that the same measurement will always format to the same string, since the unit that the quantity happens to come in as does affect the output. There is an ongoing discussion at #3418 on how to provide greater control over unit selection, but as you can see there, it is tricky... |
Beta Was this translation helpful? Give feedback.
-
|
Have filed #3440 concerning the lack of detail in the Unit documentation. |
Beta Was this translation helpful? Give feedback.
-
|
"tenths over hundreds" is perfectly fine IMO, it should have a bias towards either larger than 1, or less than 1, which ever - so that it always picks 100 vs 0.1 consistently. IMHO, "a bias toward the current unit", contradicts the idea of determining the best fitting prefix, doesn't it? |
Beta Was this translation helpful? Give feedback.
-
|
We can try out improving this rule, I agree that I prefer reading 100 over 0.1 too. Anyone interested in creating a PR to see how that works out? |
Beta Was this translation helpful? Give feedback.
-
|
I think #3418 is gradually working out an api to control/customize unit selection, which would presumably be usable both in the proposed new .toBest() method and in .format() itself. I believe the API will include the ability to turn off the unit-keeping bias and/or to prefer 100 over 0.1. Given that, it doesn't seem to me that a breaking change is warranted to modify the current unit selection rules, which will become the default in the new scheme. |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
math.unit(0.1, 'W').format({notation: 'fixed'}) gives
0.1 Wmath.unit(0.0001, 'W').format({notation: 'fixed'}) gives
100 uWI would expect either the first one to give
100 mWOR the second one to give0.1 mW.In one case is stays at 0.1 while in the other it goes to 100 lower-prefix...
Is there a way to make it consistent?
Beta Was this translation helpful? Give feedback.
All reactions