1
What I want to do is very simple, I just don’t know how to "do it right" in Javafx:
I have two Longproperty (num1 and num2) and a Doubleproperty (resultado), where this Doubleproperty resultado must contain - always updated - the value of the num1 for num2.
I can already do it with the code resultado.bind(num1.divide(num2));, but the problem is thus I lose the precision of the decimal places, and I need the decimal places.
From what I understand, the problem is that by num1.divide(num2) the method divide returns a LongBinding (because the divided variables are LongPropertys) instead of returning a DoubleBinding, and it looks like this is it LongBinding that eliminates decimal places.
I tried to make num1 and num2 also be DoublePropertyand make num1.divide(num2); and it worked: the method divide returned a DoubleBinding which preserved the decimal places in the resultado. But I want you to num1 and num2are LongPropertys even, then how to do?
I managed to get around the problem with a gambiarra that shows what I need:
import javafx.beans.binding.DoubleBinding;
import javafx.beans.binding.NumberBinding;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.LongProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleLongProperty;
public class DoubleBindingEmDivisaoDeLong {
public static void main(String[] args) {
teste1_divisaoPerdeAsCasasDecimais();
teste2_divisaoMantemAsCasasDecimais();
}
private static void teste1_divisaoPerdeAsCasasDecimais() {
final DoubleProperty resultado = new SimpleDoubleProperty(0.0);
final LongProperty num1 = new SimpleLongProperty(45);
final LongProperty num2 = new SimpleLongProperty(7);
NumberBinding divide = num1.divide(num2); // O método divide Retorna um "LongBinding" (veja a linha abaixo para confirmar)
System.out.println(divide);// Imprime: "LongBinding [invalid]"
resultado.bind(divide);
System.out.println(resultado.get()); // Imprime: "6.0" ao invés de "6.428571428571429" (perdeu as casas decimais)
}
private static void teste2_divisaoMantemAsCasasDecimais() {
final DoubleProperty resultado = new SimpleDoubleProperty(0.0);
final LongProperty num1 = new SimpleLongProperty(45);
final LongProperty num2 = new SimpleLongProperty(7);
final DoubleProperty num1Double = new SimpleDoubleProperty(0.0);
num1Double.bind(num1);
final DoubleProperty num2Double = new SimpleDoubleProperty(0.0);
num2Double.bind(num2);
DoubleBinding divide = num1Double.divide(num2Double); // O método divide Retorna um "DoubleBinding"
resultado.bind(divide);
System.out.println(resultado.get()); // Imprime "6.428571428571429" como desejado
}
}
Note that in the code above, what makes it work in test2 is the fact that I created num1Double and num2Double who are DoublePropertys and that make bind in num1 and num2 respectively, and, have done something equivalent to resultado.bind(num1Double.divide(num2Double));, that is, a gambiarra for something that should be very simple and already foreseen in Javafx.
So how do you do "the right way" in Javafx?
My resistance to change
num1andnum2forDoublePropertys is because - most likely - they will receive very large numbers (longsame), so it is not interesting for me to even cast them for double. On the other hand,resultwill receive a value of0.0until1.0, because in my real problem,num1shall always be less than or equal tonum2.– Douglas
@Douglas I understand your concern, but Double is not smaller than Long. She has a limitation, but can generally accommodate Long numbers without problems. It doesn’t get to be something like Bigdecimal, which unfortunately doesn’t seem to be part of this API (I did a brief search in Javadoc), but it should be enough for most use cases.
– utluiz
utluiz take a look at reply that I just posted (obtained from Soen), I believe it is the best way that Javafx offers to do what I asked.
– Douglas
@Douglas I really can’t judge what the best answer would be. Basically you’re turning your long variables into double underneath the scenes, so it basically has no difference in numerical accuracy. The advantage is that you keep your input variables in the type you want, the disadvantage is that your code gets more polluted.
– utluiz