This example will show how to convert a string to a double or float in groovy. Calling the string replace we will move all the special charachters. Next calling the parseFloat()
or parseDouble()
we will transform the string to a number.
String to float
@Test
void convert_string_to_number_float () {
String string = "5,873.22".replaceAll(",", "")
float convertedNumber = Float.parseFloat(string)
assert 5,873.22 == convertedNumber
}
String to double
@Test
void convert_string_to_number_double () {
String string = "2,421.22".replaceAll(",", "")
double convertedNumber = Double.parseDouble(string)
assert 5,873.22 == convertedNumber
}