开发者

How to format a number from other locale to normal en locale

I have a String "45,78". How can i format it to nor开发者_运维知识库mal format like 45.78 in java?

Probably i think i need to create some format object and than format this according to en number format.

How can i do that?


Use the predefined DecimalFormats that the JDK offers for Locales:

public static void main(String[] args) throws ParseException {
    String input = "45,78";
    NumberFormat from = DecimalFormat.getNumberInstance(Locale.FRANCE);
    NumberFormat to = DecimalFormat.getNumberInstance(Locale.US);
    String output = to.format(from.parse(input));
    System.out.println(output); // "45.78"
}

Chose Locales to suit you.

This is another case of "don't reinvent the wheel" and "use what the JDK offers"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜