0
So guys, I was "futucando" in java, but I came up with a question. How do I format a number that is in a method?
For example:
Code snippet:
JOptionPane.showMessageDialog(null,brand+ "Saldo atual de " +cliente1.getID()+" :"
+ "\n"+cliente1.getBalance());
This code prints in the format 0000.0 however I wanted to take this last decimal, already tried to use the %.4f and the %.4d, but it didn’t work out!
How do I get this one .0 of the exhibition?
Obs.: the method getBalance
is a common method of a private double.
You want to simply remove the decimal or want to round as well?
– user28595
Try this way:
JOptionPane.showMessageDialog(null, String.format("Valor: %.0f", valor));
– gato
Diego, I just want to take, and Dener, it wasn’t, because when you put that comma he understands that is a parameter more, and not a concatenation
– Daniel Santos
JOptionPane.showMessageDialog(null,brand+ "Saldo atual de " +cliente1.getID()+" :"
+ "\n"+((int)cliente1.getBalance()));
– user28595
The comma to is indicates the second parameter of the format method, with it you can format the strings using these expressions, this is not a concatenation.
– gato
See if it works using the Decimalformat class:
JOptionPane.showMessageDialog(null, "Valor: " + new DecimalFormat("####").format(valor));
– gato
For some reason the compiler takes his 4 zeros left and turns to 1 zero giving the output:
0
– gato
Please set exactly the exit rules. What happens if the decimals are not zero? You want to hide them always or only if they are zeros?
– utluiz