-
Notifications
You must be signed in to change notification settings - Fork 102
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
Display in baseScalar units without changing to SI units #23
Comments
It is not currently possible. I think implementing a method like For instance, |
I tried to write it myself and it seems to work okay. Qty('10 kW').toPrefix() // outputs quantity with '10000 W'
Qty('10 kW').toPrefix('M') // outputs quantity with '0.01 MW' I have used three external functions as I needed them for my version of toEng() as well. Here is my code: // Get prefix from unit, eg. 'kW' --> 'k'
function get_prefix(qty) {
var prefix;
if (UNITS[qty.numerator[0]][2] == "prefix") {
prefix = UNITS[qty.numerator[0]][0][0];
}
else {
prefix = "";
}
return prefix;
}
// Get unit base, eg. 'kW' --> 'W'
function get_unit_base(qty) {
var unit_base, prefix;
prefix = get_prefix(qty);
if (prefix.length === 1) {
unit_base = qty.units().substr(1);
}
else {
unit_base = qty.units();
}
return unit_base;
}
// Convert unit to defined prefix,
// eg. no prefix: '0.1 kW' --> '100 W' or prefix 'm': '0.35 W' --> '350 mW'
function convert_prefix(qty, prefix) {
var prefix, unit_base, target, qty, q;
if (prefix == undefined || prefix == null || prefix == 1) {
prefix = "";
}
unit_base = get_unit_base(qty);
target = Qty(prefix+unit_base);
q = divSafe(qty.baseScalar, target.baseScalar);
target = Qty({"scalar": q, "numerator": target.numerator, "denominator": target.denominator});
return target;
}
// ... //
assign(Qty.prototype, {
// ... //
toPrefix: function(prefix) {
return convert_prefix(this,prefix);
},
// ... //
} |
This may be easy, but I haven't quite teased out if this is possible yet.
Goal: I have data that comes in various magnitudes and I would like to normalize the the string output to the baseScalar while retuning the baseScalar's original label with the appropriate prefix change. The units are not known ahead of time so a simple someqty.to() seems to be out.
Example :
input = [1 mW, 1 W, 433 mW, 8 W]
desired output = [0.001 W, 1 W, 0.433 W, 8 W]
someqty.toBase() offers the baseScalar number, but it changes the units to SI representation. This works well for seconds and meters, but the representation of 100mW ends up being 0.1 kg*m2/s3.
One can just call someqty.baseScalar for the value, but how does one get the baseScalar's unit without transforming the label into something that is purely si units?
Am I think about this wrong? Is there a decent method converting the base scalar with only a simple prefix change on the output.
The text was updated successfully, but these errors were encountered: