2

I am developing a calculator for android. Having a double field called result, which I then put into a TextView.

If say result = 20.0 then I must represent it as 20 (without point and a zero). If result = 20.0001 it should be displayed unchanged.

Now I do it with regular expressions but I guess it's not a good idea. Is there another way to represent double without a point and a zero if double actually is an integer (without digits after the point)?

Malte Schwerhoff
  • 12,684
  • 4
  • 41
  • 71

7 Answers7

1

I hope I could understand your question. Maybe this will help you to solve your problem.

    double d = 20.0001;
    if ((int) d == d)
        System.out.println((int) d);
    else
        System.out.println(d);
Marvin
  • 55
  • 11
1

How about Explicit type casting

double y=anyValue;
int x = (int) y;
if(x==y)
log.debug(x);
else
log.debug(y);

for Input y=3.00 the output will be 3 and for Input y=3.0001 the output will be 3.0001

Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
0

Use (d % 1 == 0) { to check if your double does not have a decimal part, then use the intValue() method to get the integer out of the Double!

class testDouble {
public static void main(String[] args) {
    Double result = 20.001;
if (result % 1 == 0) {
    System.out.println(result + " Can be turned into integer, has no decimal part");
}
else {
    System.out.println(result + " Can not be turned into integer, has decimal part");
}
    result = 20.000;
if (result % 1 == 0) {
    System.out.println(result + " Can be turned into integer, has no decimal part");
    int intOfDouble = result.intValue();
    System.out.println(result + " Can be turned into integer " + intOfDouble);
}
}
}

OUTPUT :

20.001 Can not be turned into integer, has decimal part
20.0 Can be turned into integer, has no decimal part
20.0 Can be turned into integer 20
adrCoder
  • 3,145
  • 4
  • 31
  • 56
  • Thank you for a quick and exect answer –  Mar 17 '15 at 20:02
  • Glad that I could help and that it works! Please accept my answer as described here : http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work ! Thanks ! – adrCoder Mar 17 '15 at 20:02
  • Objection, your honor! OP said he has a `double` `result` not a `Double.` See [my answer](http://stackoverflow.com/a/29109020/1744774) for a one-liner; – Gerold Broser Mar 17 '15 at 20:34
0

You could output the result as a formatted string:

public static String fmt(double d)
{
    if(d == (long) d)
        return String.format("%d",(long)d);
    else
        return String.format("%s",d);
}
DigitalNinja
  • 1,078
  • 9
  • 17
0

Use the modulus % operator. Let us say your calculated value is:

double result;

if (x % 1) == 0   
    then you must have a whole number
    and you can use (int) result to cast it and int value
else //your number is not a whole number
    you will just use the result as is
axblount
  • 2,639
  • 23
  • 27
brosenlof
  • 1
  • 1
0

Check your double result against a cast (long) result and create a string to be displayed accordingly with a one-liner:

double r;
String s;
for (int i = 2; i <= 3; i++) {
    r = 100d / i;
    // this is the essential line, all others are just testing noise
    s = r > (long) r ? String.valueOf(r) : String.valueOf((long) r);
    // ------------------------------------------------------------- 
    System.out.println(s);
}

Prints:

50
33.333333333333336
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
-1

You could use modulus division like:

if((**doublevariablehere** % 1) == 0)
{
}

Code taken from here

Community
  • 1
  • 1
brso05
  • 13,142
  • 2
  • 21
  • 40