Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic Calculator Issue
#2
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.

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"
My Blog | My Setup | My Videos | Have a wonderful day.


Messages In This Thread
Basic Calculator Issue - by Vinwarez - 07-10-2012, 04:59 AM
Re: Basic Calculator Issue - by brandonio21_phpbb3_import2 - 07-10-2012, 12:16 PM
Re: Basic Calculator Issue - by Vinwarez - 07-10-2012, 01:48 PM
Re: Basic Calculator Issue - by Vinwarez - 07-11-2012, 05:31 AM
Re: Basic Calculator Issue - by Vinwarez - 07-11-2012, 11:45 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)