4
Good afternoon ! Devs, I’m having trouble solving the following problem: In a calculator app, calculations always return a Double, which automatically inserts a decimal place even if it is to deploy zero, and to show in editText I would like to remove the ". 0" when displaying the result; for example: the calculate function returns a double in this call: sum(3, 4) returns 7.0; this return I convert to String and send to Edittext.setText("7.0");
What I need is: create an algorithm that checks the last two chars of that string, if they are equal to ". 0", then I return the string without them, with an replace for example, but only if these are exactly the last two characters: This is what I have so far, but if the value is "100.07" it returns erroneously "1007";
private String convertInt(Double duplo){
return String.valueOf(duplo).replace(".0", "");
}
Simply put, what Cvoce wants is: Transforms double into string<br> Substring of last 2 characters<br> Transforms into int<br> Verifies if greater than zero:<br> yes -> printa original variable<br> not -> printa the cropped
– Matheus Guimarães Ferreira