-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit's .toString() method doesn't convert 1000 W to 1 kW #898
Comments
You can use the var c = math.unit('1000 W'); // 1000 W
var d = c.to('kW'); // 1 kW |
@ericman314 math.unit(value, 'W').toString(); and I expect it will be converted to W / kW / MW / etc depending on value |
math.js tries to format the unit using an appropriate prefix. Currently we have the following behavior:
The function chooses a prefix that results in a numeric value between approximately The details are largely a matter of preference, and we won't be able to please everybody, but if you have a suggestion to improve it, please let us know. For reference, see the function that chooses the prefix here. |
I thought with engineering notation the integer part should be less than one thousand (1-3 digits before the decimal)? Seeing as these are all SI units I can see why one would expect engineering form. It looks like just the boundary condition is wrong, (considering [Edit]
From Practical Problems in Mathematics for Information Technology, Page 67 (Except the extra prefixes between x10^-3 and 3 which should be between 1 and 9) I guess my question is: should we be using engineering notation with SI units? I think it make sense as they are commonly paired, but perhaps I'm not thinking generally enough... I see in #737 the chosen boundaries are between 0.6 and 500, I'm wondering what the thinking behind this choice is? |
I'm not sure if this is a choice I've made some day or that this was the work of @ericman314 , but it mostly boils down to heuristics and trying to output a convenient, human friendly format. The boundary at 500 is halfway two prefixes, I'm fine with further fine-tuning these heuristics if we can make it better overall, the "number between 1 and 999" rule is also nice and simple. |
I guess as ericman says this is a matter of opinion, I see maybe some people prefer to say "half a KW" instead of "500 W". Just to play devils advocate: Some people might also prefer scientific notation (exactly 1 non zero digit before the decimal), even with SI prefixes, just because of the domain they are working in... In which case I though maybe these preferences are better solved with math.unit(1000, 'W').format({notation: 'engineering'}) // "1e+3 W"
math.unit(1000.01, 'W').format({notation: 'engineering'}) // "1.00001e+0 kW" It also shouldn't really be using |
Ah, that's interesting indeed. The mechanism of units to select the best prefix is separate from formatting numbers. @ThomasBrierley would be great if you could dive into it and see if you can come up with some fixes or general improvements. |
Yes will do, I need this feature eventually and i'd rather have it as part of mathjs. I cannot promise to implement it immediately though. @pvoznyuk would this solve your issue if you were able to explicitly select engineering or scientific notation via the |
@ThomasBrierley that format method improving is a useful thing, but i'm not sure if that can help with my problem. |
My interpretation of the behaviour you want is for numbers with SI units to select the matching SI prefix for it's order of magnitude on output... in other words engineering notation. It should be possible to first fix the engineering notation formater behaviour to output the correct exponent/integer-range, and then for numbers with SI units replace the exponent with the matching prefix. e.g Using your examples this would preferably result in: 1000 W => 1 kW
1000.01 W => 1.00001 kW
999 W => 9.99 hW
1000000 W => 1 MW also 10000 kW => 10 MW
0.01 kW => 1 daW |
Closing this discussion now because it has been inactive for a long time. Feel free to reopen if needed. |
I expect 1000 W to be converted to 1 kW as well as 1000 kW is converted to 1 MW. However I see that:
Is there a way to convert 1000 W to 1 kW ?
The text was updated successfully, but these errors were encountered: