How to format a string to 2 decimal places Java?

3

I explain:

I have a String in Java :

String numero = "59.2348837";

this is what I'm trying to do:

String.format( "%.2f", numero)

And I try to get as a result:

String resultado = "59.23";

But it throws me an error because number is String . The error jumps when I do string format .

How could you format that String to 2 decimals and that the result remains a String ?

    
asked by Luis Fernando 18.04.2018 в 00:14
source

4 answers

3

marks you error "number is String", since you need to convert the String to a numeric value, you can do it in this way converting the value of String to Double or Float by Double.parseDouble(numero) or through Float.parseFloat(numero) :

String numero = "59.2348837";

String resultado = String.format( "%.2f", Float.parseFloat(numero));

There is a similar question

Show number with two decimals

but in this case you can convert the value of String to Double by Double.parseDouble(numero) , example:

   String numero = "59.2348837";

   DecimalFormat df = new DecimalFormat("0.00");
   String resultado = df.format(Double.parseDouble(numero));

result will have the value:

59.23

You can even use a method with the above described to obtain the value you require:

private static String getTwoDecimals(String valor){
    DecimalFormat df = new DecimalFormat("0.00");
    return df.format(Double.parseDouble(valor));
}

and you call it this way:

String numero = "59.2348837";
String resultado = getTwoDecimals(numero);

to obtain as a result value:

59.23
    
answered by 18.04.2018 / 00:27
source
2

It's because the %.2f format is allowed for float. To format them you will have to convert the string to float and then format it:

String numero = "59.2348837";
String result = String.format( "%.2f", Float.parseFloat(numero)); // <- convertiendo el string a float
System.out.println(result); // 59.23
    
answered by 18.04.2018 в 00:21
2

The problem you are having is that in the format string, you are telling the String.format() method that what you will receive as the first parameter is a floating number and when you pass it a String you should throw a similar exception to this:

'java.util.IllegalFormatConversionException: f != java.lang.String'

To solve this, you only have to convert your string to a floating number (float or double) and pass that number to the method. This way it helps you:

String numero = "59.2348837";
String numeroFormateado = String.format("%.2f", Double.parseDouble(numero));
    
answered by 18.04.2018 в 00:26
1

If you only want 2 decimals you can also do a substring so you save the double pairing of String to double and double to String

I'll give you a quick example:

// Aqui tenemos el número
String numero = "100.9988" 

// Despues obtenemos la posición del punto
int posicionPunto = numero.indexOf('.'); 

// Seguidamente coges solo lo que te interesa del String desde el inicio hasta los2 decimales
// Le sumamos 3 a posicionPunto para que avance 3 posiciones (1 Punto y 2 Decimales)
resultado = numero.subString(0,posicionPunto+3) 
    
answered by 18.04.2018 в 11:38