Search code examples
javajavafxdivisionjavafx-bindings

How to not lose decimal places when doing "longProperty1.divide(longProperty2)"?


I have two LongProperty (there are no decimal places in a long) variables (longProperty1 and longProperty2). I need to divide them and I need to keep the division result in a DoubleProperty (there are decimal places in a double) variable (result) without losing decimal places.

final DoubleProperty result = new SimpleDoubleProperty(0.0);
final LongProperty longProperty1 = new SimpleLongProperty(45);
final LongProperty longProperty2 = new SimpleLongProperty(7);
result.bind(longProperty1.divide(longProperty2));
System.out.println(result.get()); //print: "6.0" instead of "6.428571428571429" (lost decimal places)

longProperty1 and longProperty2 are of type LongProperty because they receive very large numbers, so I can't cast them to DoubleProperties.

On the other hand, the result will receive a value from 0.0 to 1.0 (for example: 0.0, 0.2975, 0.5378, 0.9853, 1.0), because in my real problem longProperty1 <= longProperty2(for example: 500/600=0.8333, 1/2=0.5, 1897/5975=0.3174, ...).

How to do this?


Solution

  • The methods provided LongProperty (and by other NumberExpressions) such as divide are convenience methods. You can create a custom binding that does whatever you want:

    final DoubleProperty result = new SimpleDoubleProperty(0.0);
    final LongProperty longProperty1 = new SimpleLongProperty(812323534);
    final LongProperty longProperty2 = new SimpleLongProperty(956745323);
    result.bind(Bindings.createDoubleBinding(() -> longProperty1.longValue() / (double) longProperty2.longValue(),
        longProperty1, longProperty2));
    System.out.println(result.get());
    longProperty1.set(612323534);
    System.out.println(result.get());
    

    Bindings is a utility class for creating bindings. Here I created a custom binding that returns a double by casting the division of longs to double. You will lose precision here.

    The output of the above is:

    0.8490488685670847
    0.6400068223798362