Format Number Java

Asked

Viewed 225 times

0

I’m studying java and doing some exercises. I am doing an exercise where I passed 2 values to be formatted, However when I run the program gives the error below, if I step only 1 value works, but with 2 gives the error.

Exception in thread "main" java.lang.Numberformatexception: Multiple points at sun.misc.Floatingdecimal.readJavaFormatString(Unknown Source) at sun.misc.Floatingdecimal.parseDouble(Unknown Source) at java.lang.Double.parseDouble(Unknown Source) at Testemain.formatNumer(Testemain.java:37) at Testemain.main(Testemain.java:22)

Below follows the code, in case someone can give me a hint thank you

public static void main(String[] args) {

        double salario = 4520;
        double resto = 0;
        double imposto1 = 0;
        double imposto2 = 0;
        double impostoAPagar = 0;



        if(salario > 4500){
            resto = formataNumero(salario % 4500); //guarda o resto da divisao de 4520 por 4500
            salario = salario - resto;
            imposto1 = (salario - 3000) * 0.18; // 
            imposto2 = salario - (salario - 3000) * 0.08;

            impostoAPagar = imposto1 + imposto2;
            impostoAPagar = formataNumero(impostoAPagar);

            System.out.println("Numero formatado: "+resto);
            System.out.println("Imposto a pagar: "+impostoAPagar);


        }
    } //Fim do main

    public static double formataNumero(double valor){
            double numeroFormatado = 0;
            DecimalFormat formatador = new DecimalFormat();

            formatador.applyPattern("###,###.00; (###,###.00)");

            return Double.parseDouble(formatador.format(valor).replaceAll(",", "."));



    }

}
  • I didn’t know you could format like this applyPattern("###,###.00; (###,###.00)"). I believe the problem is caused by replaceAll(",", "."). When calling this method your number is, for example: 123.456.00. If we convert to br format it’s like 123,456,00. You can’t have two commas.

  • Um interesting, so I used the replaceAll because when I print using the println gives another error claiming about the comma so I will say that I converted from comma to dot to give right the impression.

  • I get it. But if you remove the replaceAll() and debug, the variable value impostoAPagar is that correct? Put a brakepoint on the line System.out.println("Numero formatado: "+resto); and see the value of impostoAPagar.

  • I will check, actually I changed the code here and now I printed normal, now I’m just breaking my head with Exercise because the result is not what the judges kkkk expected for me is all right, but how they do various tests is not 100% right, but vlw by help...

  • I don’t understand why you take a double, format it and then return it to a double. If you want to read a string in pt_BR format and turn it into a number, use NumberFormat instance = DecimalFormat.getInstance(new Locale("pt", "BR")); and then Number parse = instance.parse(valor);

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.