Android Fill in with decimals

2

I have a report where I show certain numerical values that come to me from a service x,

My problem in particular is that if the number is 5.10

the service sends me 5.1

If I send 5.00 the service brings me 5

But if I send 5.23 if I get 5.23, what I want to do is:

that if it arrives 5.1 it is completed with 5.10, if it arrives with 5 that leaves it as 5.00, I thought about doing this with 2 IFS and ask the number of characters and add the 0 but it seems ineffective or very precarious, there is some How to perform this process?

 final ArrayList<MovimientoCuenta> items = new ArrayList<>();


int cantidad  = response.getMessageRS().getData().size();
                int iterador = 0 ;
                while(iterador<cantidad) {
                    MovimientoCuenta aux2 = new MovimientoCuenta(response.getMessageRS().getData().get(iterador).getDST(),
                            "USD "+response.getMessageRS().getData().get(iterador).getAMOUNT(), // este es el monto
                            response.getMessageRS().getData().get(iterador).getCURRENCY(),
                            response.getMessageRS().getData().get(iterador).getDATE(),""
                            ,response.getMessageRS().getData().get(iterador).getREFERENCE(),"",
                            response.getMessageRS().getData().get(iterador).getSIGN(),
                            response.getMessageRS().getData().get(iterador).getNAME());
                    items.add(aux2);
                    iterador ++;
                }

I already tried

DecimalFormat formater = new DecimalFormat("###.00");

       mount.setText(item.getMoneda() + " "+formater.format(Double.parseDouble(item.getMonto())));
    
asked by Bruno Sosa Fast Tag 16.01.2018 в 20:40
source

2 answers

2

You can convert to double from the String and apply a format that applies double decimal "0.00" , example:

  DecimalFormat df = new DecimalFormat("0.00");
  String numeroString = "5.1";
  double num =  Double.parseDouble(numeroString);       
  System.out.println ("Resultado: " + df.format(num));

You would have as a result:

"5.10"

The same for the other values:

    DecimalFormat df = new DecimalFormat("0.00");

    String numer = "5.1";
    double num =  Double.parseDouble(numer);        
    System.out.println ("Original: "+ numer + " Resultado: " + df.format(num));
    //Original: 5.1 Resultado: 5.10

    numer = "5.23";
    num =  Double.parseDouble(numer);       
    System.out.println ("Original: "+ numer + " Resultado: " + df.format(num));
    //Original: 5.23 Resultado: 5.23

    numer = "5";
    num =  Double.parseDouble(numer);
    System.out.println ("Original: "+ numer + " Resultado: " + df.format(num));
    //Original: 5 Resultado: 5.00

Applied to your Android code:

 DecimalFormat formater = new DecimalFormat("0.00");
 mount.setText(item.getMoneda() + " "+formater.format(Double.parseDouble(item.getMonto())));
    
answered by 16.01.2018 / 21:47
source
3

Use the String.format() method to format the value:

System.out.println(String.format("%.2f", Double.parseDouble("5.10"))); // 5.10
System.out.println(String.format("%.2f", Double.parseDouble("5.23"))); // 5.23
System.out.println(String.format("%.2f", Double.parseDouble("5.0")));  // 5.0

%.2f means the following:

% = Numero
2f = 2 numeros flotantes
    
answered by 16.01.2018 в 21:23