Format String

2

I have the following code:

String convertedString = new DecimalFormat("##.###.###,##")
    .format(Double.parseDouble(Constantes.TRANSACCION_MONTO));
TXTTRANSMONTO.setText(convertedString);

How do I make the app not crashee? I get an error.

The constant TRANSACCION_MONTO is string with value 150000, but you want it to display 150,000.00

    
asked by Jesus Moran 13.08.2017 в 18:27
source

2 answers

1

In this case it is important to have an appropriate value that is transformed by the defined pattern that in this case is "##.###.###,##" otherwise you will have a IllegalArgumentException that can be a cause of the closure of your application.

Another reason is that if you are converting to Double, firstly ensure that the String value to be converted into a numerical one, otherwise you would have error NumberFormatException .

You comment that the value of TRANSACCION_MONTO is "150000", and you want to show "150,000.00", for this the correct pattern to apply the format should be "###, ###. 00"

Example:

 String convertedString = new DecimalFormat("###,###.00")
                .format(Double.parseDouble(TRANSACCION_MONTO));

This way, convertedString will have a value:

 150,000.00
    
answered by 13.08.2017 / 19:28
source
-1

A very simple way to convert any value to String, is to concatenate it with a string of text or even with two empty quotes, the question is to concatenate it.

Ex:

int numero = 256;
String texto = "" + numero;

// En este caso imprimiria el número como una cadena de texto.
Log.i("log", texto);

It can be any type of value, such as a Boolean value:

boolean condicion = true;
String texto2 = "" + condicion;

// En este caso imprimiria el valor booleano como una cadena de texto.
Log.i("log", texto2);
    
answered by 14.08.2017 в 03:58