07-10-2012, 12:16 PM
Well, there is one easy solution to this, and as you probably predicted: You need to change the integers to doubles. Although doubles give you ".0" and you don't want that, you simply need to use doubles in order to keep track of decimals. By using integers in a calculator, your calculator is only correct when doing math with no remainders or decimals.
In order to solve this problem, you're going to want to use a NumberFormatter. So, for example, say your answer double is res. You then need to use the following code segment:
So now all you have to do is fill the textbox with the String answer, and you're good to go!
Edit: I have just tested this and it does indeed work: 4/2 displays "2", while 4/3 displays "1.333"
Vinwarez Wrote:I assume you will tell me "Just change integers to doubles", but I don't want that. I want it to display any decimals except zero. Example:
4 / 2 = 2(.0) | Don't display ".0"
4 / 3 = 1(.3) | Display ".3"
In order to solve this problem, you're going to want to use a NumberFormatter. So, for example, say your answer double is res. You then need to use the following code segment:
Code:
NumberFormat df = DecimalFormat.getInstance();
df.setMinimumFractionDigits(0);
df.setRoundingMode(RoundingMode.DOWN);
String answer = df.format(res); //This is the new //formatted variable that doesn't include the .0 at the
//end of it
So now all you have to do is fill the textbox with the String answer, and you're good to go!
Edit: I have just tested this and it does indeed work: 4/2 displays "2", while 4/3 displays "1.333"