How to remove decimal point

Asked

Viewed 1,497 times

0

I have this method

   public void calcularVotosTotal(){

    System.out.println("votos valiudos " + this.getNumeroEleitores() * 0.8 + "% " + " Votos Brancos "
                        + this.getNumeroEleitores() * 0.06 + "% " + " votos nulos "
                        +this.getNumeroEleitores() * 0.14 + "% ");
}

I have here the main method

 public static void main(String[] args) {
    AlgoritimoNumeroTotalPessoas antp = new AlgoritimoNumeroTotalPessoas(100);
    antp.totalEleitores();
    antp.calcularVotosTotal();
}

and here the console output below, how do I remove the points in the decimal place? and leave the output so 80%, 6%, 14%

Numero total de eleitores 100
votos validos 80.0%  Votos Brancos 6.0%  votos nulos 14.000000000000002% 
  • I know there are some classes like format, Bigdecimal, but I didn’t know how to use this structure, because I’m beginner yet.

  • What the method getNumeroEleitores returns?

  • getNumeroElectors method is only a java bean of the numero attribute which is double type

2 answers

1

From what I saw, you want to return an integer value, I think the "problem" is in the return of the method getNumeroEleitores, that should actually be a long, I imagine you’re returned a double or float, in that case you just need to perform a conversion to a long, follows an example below:

public class HelloWorld{
     public static void main(String []args){
        double d = 15.5;
        System.out.println((long) d); //saida: 15
     }
}

Note: I used the long for fear of bursting the maximum value of int.

0


It worked I was doing the cast the wrong way, obg to all!

public void calcularVotosTotal(){

    System.out.println( "votos validos "  + (int)(this.getNumeroEleitores() *  0.8) + "% " + " Votos Brancos "
                        + (int)(this.getNumeroEleitores() * 0.06) + "% " + " votos nulos "
                        + (int)(this.getNumeroEleitores() *  0.14) + "% ");
}

Browser other questions tagged

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