我有一个标签和一个双重属性。我想输出带有后缀“$”的值,i。 e. “200.50 美元”。我如何使用 JavaFX 做到这一点?我想过像这样使用绑定(bind):
@FXML Label label;
DoubleProperty value;
...
Bindings.bindBidirectional( label.textProperty(), valueProperty(), NumberFormat.getInstance());
但还没有找到将“$”附加到文本的方法。
非常感谢您的帮助!
最佳答案
这样的事情应该可行。
Bindings.format("%.2f $", myDoubleProperty.getValue());
现在可以绑定(bind)了
label.textProperty.bind(Bindings.format("%.2f $", myDoubleProperty.getValue());
如果您想使用 NumberFormat 实例,只需用它格式化 myDoubleProperty 的值即可。
NumberFormat formatter = NumberFormat.getInstance();
formatter.setSomething();
formatter.format(myDoubleProperty.getValue());
现在将它添加到我们的 Bindings.format 中。
编辑:
包括 ItachiUchiha 的输入。
关于binding - 如何使用格式化程序绑定(bind)属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29028136/