What is my goal:
我正在寻找一种在 Java 中对大数添加 (.) 的方法。经过研究我制定了以下方法:
/**
* @param number
* @return A number with more than 3 digits [ Example 1000 as 1.000] with dots every 3 digits
*/
public static String getNumberWithDots(int number) {
return String.format(Locale.US, "%,d", number).replace(",", ".");
}
The problem:
现在我在 JavaFX 中有一个 Label
。我们将其命名为 label
。实际上我想将其文本绑定(bind)到 SimpleIntegerProperty
让其命名为 totalCats
,但我想格式化文本,以便它实际上符合上面的方法。
我的意思是,如果 totalCats 是 1000 ,我希望标签显示 1.000 。到目前为止,我只实现了1,000,但我不知道如何用(.)替换(,),因为StringBinding
没有替换方法:
label.textProperty().bind(totalCatsProperty().asString(Locale.US, "%,d"));
Finally:
感谢任何帮助:)
最佳答案
Bindings::create****Binding
在这种情况下很有用。
label.textProperty().bind(Bindings.createStringBinding(
() -> String.format(Locale.US, "%,d", totalCatsProperty().get()).replace(",", "."),
totalCatsProperty()));
关于JavaFX 在 JavaFX StringBinding 中将逗号 (,) 替换为点 (.),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43750142/