mardi 24 février 2015

Efficient way of adding zeros to a String number?



I would like to turn all values into the following number format, x.xxx. I am doing this fine by using a while loop and concatenating 0 to string values that do not have the correct length. However, I would like to know if there is a more efficient way of doing this. To be more clear of what I am trying to do, here are some examples.


Ex1--Change a string value such as 2.5 to 2.500 Ex2--Change a string value such as 2.51 to 2.510.


This is how I am currently doing this.



while (str.length() < 5) {
str= str+ "0";
}



Aucun commentaire:

Enregistrer un commentaire