What I want to do is very simple, I just do not know how to "do it right" in JavaFX:
I have two LongProperty ( num1
e num2
) and a DoubleProperty ( resultado
) , where this DoubleProperty resultado
must contain - always updated - the value of the division of num1
by num2
.
I can already do this with the code resultado.bind(num1.divide(num2));
, but the problem is that this way I lose the precision of the decimal places , and I need the decimal places.
From what I've noticed, the problem is that doing num1.divide(num2)
the method divide
" returns an LongBinding
(because split variables are LongProperty
s) instead of returning a DoubleBinding
, and it seems like this LongBinding
that deletes the decimal places.
I tried to make num1
and num2
also be DoubleProperty
and make num1.divide(num2)
; and worked: the divide
method returned a DoubleBinding
that preserved the decimal places in resultado
. But I want num1
and num2
to be LongProperty
s, so how do I do it?
I've 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 works in test2 is the fact that I have created num1Double
and num2Double
which are DoubleProperty
if they 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 provided in JavaFX.
So how do you do the "right way" in JavaFX?