java String.format:具有本地化的数字 [英] java String.format: numbers with localization

查看:149
本文介绍了java String.format:具有本地化的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以像NumberFormat.format一样以String.format调用来本地化数字?

Is it possible to localize numbers in String.format call the same way as NumberFormat.format does?

我期望它只是使用

String.format(locale, "%d", number)

,但这不会返回与使用NumberFormat相同的结果.例如:

but this doesn't return the same result as if using NumberFormat. For example:

String.format(Locale.GERMAN, "%d", 1234567890) 

给出:"1234567890",而

gives: "1234567890", while

NumberFormat.getNumberInstance(Locale.GERMAN).format(1234567890)

给出:"1.234.567.890"

gives: "1.234.567.890"

如果无法完成,建议对包含数字的文本进行本地化的推荐方式是什么?

If it can't be done, what's recommended way for localizing text including numbers?

推荐答案

来自

  • 提供语言环境(如您在示例中所做的那样)
  • 包括','标志以显示特定于语言环境的分组分隔符
  • 因此您的示例将变为:

    String.format(Locale.GERMAN, "%,d", 1234567890) 
    

    请注意在'd'之前的附加','标志.

    Note the additional ',' flag before the 'd'.

    这篇关于java String.format:具有本地化的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆