【问题标题】:How do I add commas to numeric strings in an Anylogic presentation text object?如何将逗号添加到 Anylogic 演示文本对象中的数字字符串?
【发布时间】:2021-11-28 07:59:59
【问题描述】:

我在 Anylogic 演示文稿中有一个文本对象,其代码如下: "Infrastructure costs: £" + Math.round(flowOpRoomsPCN) + "/day"

此文本显示:基础设施成本:123456789 英镑/天

我希望这样显示:基础设施成本:123,456,789 英镑/天

似乎是一个简单的问题,但在任何地方都找不到答案。

【问题讨论】:

    标签: anylogic


    【解决方案1】:

    您还可以使用出色的 Java 字符串格式化程序:

    double value = 123_456_789.00d;
    String output = String.format("Infrastructure costs: £%(,.2f/day", value);
    

    上述格式化指令%(,.2f 指示字符串格式化程序接受value 并将其表示为###,###.## 正数模式和(###,###.00)为负数。

    更多信息请参见 Java API 文档here

    【讨论】:

    • 感谢 Artem,这很好用而且很简单。
    【解决方案2】:

    您可以使用标准的 Java 格式

    DecimalFormat formatter = new DecimalFormat("#,###.00");

    "Infrastructure costs: £" + formatter.format(Math.round(flowOpRoomsPCN)) + "/day";
    

    【讨论】:

    • 感谢 Jaco-Ben,我接受了另一个答案,因为它对我来说似乎更直接。但我想你的解决方案可能同样有效。
    • 不用担心,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    相关资源
    最近更新 更多