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.
我有两个LongProperty(长整数中没有小数位)变量(longProperty1和longProperty2)。我需要划分它们,我需要将除法结果保存在DoubleProperty(在double中有小数位)变量(result)而不会丢失小数位。
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
.
longProperty1和longProperty2属于LongProperty类型,因为它们接收的数字非常大,所以我无法将它们转换为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
, ...).
另一方面,结果将从0.0到1.0接收一个值(例如:0.0,0.2975,0.5378,0.9853,1.0),因为在我的实际问题中longProperty1 <= longProperty2(例如:500/600 = 0.8333,1 /2=0.5,1897/5975 = 0.3174,......)。
How to do this?
这该怎么做?
2 个解决方案
#1
1
The methods provided LongProperty
(and by other NumberExpression
s) such as divide
are convenience methods. You can create a custom binding that does whatever you want:
LongProperty(和其他NumberExpressions)提供的方法如divide是方便的方法。您可以创建一个可以执行任何操作的自定义绑定:
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 long
s to double
. You will lose precision here.
Bindings是一个用于创建绑定的实用程序类。在这里,我创建了一个自定义绑定,通过将long的除法转换为double来返回double。你在这里会失去精确度。
The output of the above is:
以上的输出是:
0.8490488685670847
0.6400068223798362
#2
0
You should use BigDecimal
for your calculations.LongProperty
is just a wrapper for usual long value, so no decimal points.
您应该使用BigDecimal进行计算。 LongProperty只是通常长值的包装器,因此没有小数点。
You can try doing longProperty1.divide(longProperty2.doubleValue())
as well, which would call DoubleBinding divide(final double other)
, but I think this would remove the point of using LongProperty
.
您也可以尝试使用longProperty1.divide(longProperty2.doubleValue()),这将调用DoubleBinding除法(最后双倍),但我认为这将删除使用LongProperty的点。
#1
1
The methods provided LongProperty
(and by other NumberExpression
s) such as divide
are convenience methods. You can create a custom binding that does whatever you want:
LongProperty(和其他NumberExpressions)提供的方法如divide是方便的方法。您可以创建一个可以执行任何操作的自定义绑定:
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 long
s to double
. You will lose precision here.
Bindings是一个用于创建绑定的实用程序类。在这里,我创建了一个自定义绑定,通过将long的除法转换为double来返回double。你在这里会失去精确度。
The output of the above is:
以上的输出是:
0.8490488685670847
0.6400068223798362
#2
0
You should use BigDecimal
for your calculations.LongProperty
is just a wrapper for usual long value, so no decimal points.
您应该使用BigDecimal进行计算。 LongProperty只是通常长值的包装器,因此没有小数点。
You can try doing longProperty1.divide(longProperty2.doubleValue())
as well, which would call DoubleBinding divide(final double other)
, but I think this would remove the point of using LongProperty
.
您也可以尝试使用longProperty1.divide(longProperty2.doubleValue()),这将调用DoubleBinding除法(最后双倍),但我认为这将删除使用LongProperty的点。