Formatted string to number

This example will show how to convert a formatted number stored string to a number by parsing it as text using DecimalFormat. When you know the format of a given string you can pass it the constructor of DecimalFormat. Then by calling the DecimalFormat.parse method it will convert the string into a number. Be aware that if the number does not match the format a ParseException will be thrown so handle appropriately.

Straight up Java

@Test
public void convert_formatted_string_to_number_java () throws ParseException {
    DecimalFormat df = new DecimalFormat("#,##0.00;(#,##0.00)");
    
    Number parsedNumber = df.parse("123,234.25");
    
    assertEquals(123234.25, parsedNumber);
}