-
-
Notifications
You must be signed in to change notification settings - Fork 41
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
Convert Quantity to most fitting Prefix #236
Comments
Converting not really, but |
Well, I meant it for formatting purposes, |
Ok so if it's about formatting (potentially with confersion) then please refer to indriya#189. We start exploring these ideas with implementations of |
I wrote a small piece of code to better illustrate what I'm trying to do: public static Quantity<?> unsafeConvert(Quantity<?> quantity, Unit<?> unit) {
Number value = quantity.getValue();
UnitConverter converter;
try {
converter = quantity.getUnit().getConverterToAny(unit);
} catch (IncommensurableException e) {
throw new RuntimeException(e);
}
return Quantities.getQuantity(converter.convert(value), unit);
}
public static Quantity<?> simplifyUnits(Quantity<?> quantity) {
Prefix[] large = new Prefix[] { KILO, MEGA, GIGA, TERA, PETA, EXA, ZETTA, YOTTA };
Prefix[] small = new Prefix[] { MILLI, MICRO, NANO, PICO, FEMTO, ATTO, ZEPTO, YOCTO };
Unit<?> baseUnit = quantity.getUnit().getSystemUnit();
if (baseUnit == Units.KILOGRAM) baseUnit = Units.GRAM; // special case for kg
quantity = unsafeConvert(quantity, baseUnit);
double value = quantity.getValue().doubleValue();
int log = (int)Math.floor(Math.log(value) / Math.log(1000));
if (log == 0) return quantity;
Prefix prefix;
if (log < 0) {
log = Math.min(-log, small.length);
prefix = small[log-1];
} else {
log = Math.min(log, large.length);
prefix = large[log-1];
}
Unit<?> targetUnit = baseUnit.prefix(prefix);
return unsafeConvert(quantity, targetUnit);
}
public static void main(String[] args) {
double[] test = new double[] { 42*Math.pow(10, -6), 0.1, 1, 4500, Math.pow(10, 6) };
for (double t : test) {
Quantity<?> result = simplifyUnits(Quantities.getQuantity(t, Units.GRAM));
System.out.println(result);
}
} Outputs:
Perhaps a solution would be to integrate something like this inside a custom QuantityFormatter as you suggest. I was just wondering if there was a built-in way before rolling my own, as this seems like something that would come up often. Edit: Ironed out some kinks in the code. |
I think I only half-guess how these values are related (not quite |
It's just a few random numbers to show how you could simplify the value to use more precise units instead of showing a very large (or very small) value. Similarly to how your computer doesn't tell you a file weighs 44500 bytes but may instead say 44.5 kb, you may want the Quantity object to print the latter. |
I will transfer this to Indriya to explore possible functions like that. |
Is there a way to convert a Quantity to the most fitting Prefix?
e.g. given MetricPrefix:
1000 m
to1 km
,0.1 m
to1 cm
The text was updated successfully, but these errors were encountered: