You can use DecimalFormat
to parse and format decimal numbers.
There are separators for thousands of separators. Here's a simple example that deliberately parses on a BigDecimal
, and not on a float
or double
- a binary floating point, not suitable for currency values.
import java.math.*; import java.text.*; public class Test { public static void main(String[] args) throws Exception { DecimalFormat format = new DecimalFormat(); format.setParseBigDecimal(true); BigDecimal value = (BigDecimal) format.parse("1,234.56"); System.out.println(value); } }
Now that you are only using the default locale for things like the thousands separator and decimal separator, you might want to specify a specific locale if you know what will be used for the file you need to parse.
source share