2

我想格式化一条消息,如“1.0 KiB 的数据传输需要 1:32 小时”。但我需要格式化和本地化的字节数和持续时间。我发现了几个如何格式化字节数的想法,持续时间似乎很好。但随之而来的是本地化,我的本土代码变得过多。我正在寻找如何在 ICU 库的帮助下实现这种格式化的任何想法。

4

2 回答 2

2

您可以简单地使用 Java MessageFormat,它允许您将不同的 Java 类型格式化为字符串;这种格式应该做你想做的

final MessageFormat format = new MessageFormat("Data transfer of {0,number,0.0} KiB took {1}:{2} hours");

用法是

final String formatted = format.format(new Object[]{amountTransferred, hours, minutes});

哪里amountTransferred是 a doubleor floator BigDecimalhours并且minutes是整数类型。

如果您需要单独执行此操作,已经有一篇关于如何格式化分钟数的帖子。HH:mm

于 2013-03-09T13:55:56.877 回答
0

ICU has both compact decimals for units (1.0 MiB) and duration formats now. This is a great question, I don't think msgformat supports this natively now.

What you could do is to just use the pattern {0,number} without KiB as in Boris' response, but then call format.setFormat(0,...) to replace the number formatter with a CompactDecimalFormat or with a unit formatter.

于 2013-09-05T23:54:12.783 回答