如何解析包含逗号的数字字符串到java中的整数? [英] How to parse number string containing commas into an integer in java?

查看:579
本文介绍了如何解析包含逗号的数字字符串到java中的整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用 Integer.parseInt()解析265,858时,我得到 NumberFormatException



有没有办法解析成一个整数?

解决方案

这是逗号一个小数分隔符还是这两个数字?在第一种情况下,您必须提供 区域设置 NumberFormat 使用逗号作为小数分隔符的类:

  NumberFormat.getNumberInstance(Locale.FRANCE).parse(265,858)

这导致 265.858 。但是使用美国语言环境,您将获得 265858

  NumberFormat.getNumberInstance (java.util.Locale.US).parse(265,858)

这是因为在法国他们在美国将逗号作为小数分隔符处理 - 作为分组(千)分隔符。



如果这两个数字 - String.split() code>他们并独立解析两个独立的字符串。


I'm getting NumberFormatException when I try to parse 265,858 with Integer.parseInt().

Is there any way to parse it into an integer?

解决方案

Is this comma a decimal separator or are these two numbers? In the first case you must provide Locale to NumberFormat class that uses comma as decimal separator:

NumberFormat.getNumberInstance(Locale.FRANCE).parse("265,858")

This results in 265.858. But using US locale you'll get 265858:

NumberFormat.getNumberInstance(java.util.Locale.US).parse("265,858")

That's because in France they treat comma as decimal separator while in US - as grouping (thousand) separator.

If these are two numbers - String.split() them and parse two separate strings independently.

这篇关于如何解析包含逗号的数字字符串到java中的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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