0

I am receiving points from database that is integer. After receiving points i am doing some calculation and showing result to user. Assume i received 20, 21, ,22. I am doing 21/1.5+20+22 which results in 43.50, But some time the points i receives is all even so the result will be like 50.00. The Problem Is i want to show 43.50 if decimal is not 00 And 50 If decimal is 00.. What data type should i use for calculation and show results like i mentioned above.THANKS. Currently I am using this but using integer wont show the decimal

int tot = a1+a2+a3+a4+a5+a6+a7+a8+a9+a10+a11+a12+a13+a14+a15+a16+a17+a18+a19+a20+a21+a22+a23+a24;
Zaid Khan
  • 5
  • 2

6 Answers6

1

Save the output in double datatype, and while showing put checks on it like this:

    Double result;
    if (result%1.0 == 0.0){
        // show result.intValue();
    } else {
        // show result System.out.printf("result : %.2f%n", result);
    }
Rajat Mehra
  • 1,462
  • 14
  • 19
0

If you just want to present with exactly 2 decimal places you can use DecimalFormat e.g. and for calculating float point values you can use double or float type

DecimalFormat df   = new DecimalFormat("#,##0.00");
String formatedTot = df.format(tot);
System.out.println(formatedTot);
Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
0

From the description it's hard to point you in the correct direction.

Please show some code how you show the results in Android.

Regarding datatype it should be clear that you can't convert an integer to a decimal because you can't magically add decimal values to it. Therefore I guess you need a decimal datatype and need to check if it's an integer which is explained here: How to test if a double is an integer

Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
0

Your issue is not in calculation, but in displaying the result. When you use int or Integer for your calculation, Java will always round down. Even float and double are only approximations (see here). Depending on how accurate you result should be use float (medium accuracy), double (ok accuracy) or BigDecimal (best accuracy).

To solve your issue, calculate your number as precise as required and define while printing how it should be represented as described here.

Bernard
  • 333
  • 2
  • 8
0

You could use double data type or float data type. If you need to check the remainder after the decimal point you could use something like,

            double num = 50.00;
            if (num % 1.0 == 0.0){
            int numOne = (int) num;
            System.out.println(numOne)
            }

The above code (by Rajat Mehra) which I edited, will cast values with "0" remainder to an integer and display them accordingly.

ArbitraryChoices
  • 243
  • 1
  • 11
0

Your input is a set of integers. Your question implies you do not want rounding so the result of your calculation should be a float or double.

double result = calculate(integers);

You note that you want to show the fractional portion of the calculated value even if the result is an integer. Java has a variety of options to format the output. One simple approach is using printf.

System.out.printf("result=%.2f%n", result);
vsfDawg
  • 1,425
  • 1
  • 9
  • 12