-3

If I have a double storing 10.0 what is the best way to convert it to string 10?
Currently I do:

if (object.getValue() > 9.999) {
  someObject.setText(String.format("%d", new Double(object.getValue()).intValue()));
}
else {
    //use amount as is
}

Update
These are class grade scores. I only care for a good coding style for checking out if a double is 10.0 and only then convert it to the string “10”. Don’t care about any other number

Jim
  • 18,826
  • 34
  • 135
  • 254
  • @AvinashRaj:What do you mean? Cast? – Jim May 20 '15 at 13:21
  • You've specified how you want 10.0 to be represented - what about 10.5? 10.25? 10.125? – Jon Skeet May 20 '15 at 13:21
  • @JonSkeet: I don't go higher than 10.0 – Jim May 20 '15 at 13:22
  • @Jim: I'm not sure how we would be expected to know that... but it still doesn't particularly help. What about 9.5? 9.75? 9.875? – Jon Skeet May 20 '15 at 13:26
  • @JonSkeet:You are right. My bad. These are class grade scores. I **only** care for a good coding style for checking out if a double is 10.0 and only then convert it to the string “10”. No other number – Jim May 20 '15 at 13:30
  • @Jim: Well those precise requirements are met by `someObject.setText(object.getValue() == 10 ? "10" : "apparently this doesn't matter");` - but I suspect you really *do* care about other numbers. – Jon Skeet May 20 '15 at 13:32
  • @JonSkeet:I can not do `==` on floats/doubles AFAIK. They are never equal – Jim May 20 '15 at 13:33
  • 1
    @Jim: Yes you can, if you know that the value will be *exactly* 10.0, which is what you said you care about. Now if you want to know whether it's *roughly* 10.0, that's a different matter. – Jon Skeet May 20 '15 at 13:34
  • @JonSkeet:I know that I get a bunch of numbers (doubles) and some will be 10.0. The `object.getValue()` returns a double. I think using `object.getValue() == 10.0` is not a safe approach because of floats. I don’t know if 10.0 can be exactly represented for example or if someone will introduce some code style checks and I will get warnings about this – Jim May 20 '15 at 13:37
  • 1
    @Jim: Yes, 10.0 *can* be exactly represented as a `double` (and that's easy to demonstrate). If your values won't be *exactly* 10.0, but instead some `double` value *near* 10.0, you need to put some sort of tolerance in. But you haven't explained your requirements around that - or indeed anything, really. – Jon Skeet May 20 '15 at 13:39
  • @JonSkeet: If the number is 10.0 convert it to the string “10”. Best coding style. I don’t care about 9.25 or 9.89 or 10.02 etc. I think I am confusing you because it might be trivial for you :( – Jim May 20 '15 at 13:57
  • 1
    @Jim: As I've said, if you only care about the exact value of 10.0, then `if (x == 10.0)` expresses *exactly* what you mean. – Jon Skeet May 20 '15 at 14:01

6 Answers6

0

If you know the string, you can simply substring it until the dot:

String s = "10.0";
System.out.println(s.substring(0, s.indexOf('.')));

If you want to manipulate it as a double (I don't really see why, though), you can try something like:

System.out.println(String.format("%d", (int)Math.floor(new Double("10.0"))));

There are plenty of ways, of course. I think overall your best bet is with a substring, but I'm not sure what your needs are.

Hope this helps :)


UPDATE after question clarification:

If all you're interested is checking the value of a double (assuming of course, object.getValue() is a double), just check:

if (object.getValue() >= 10)

It will cover whatever you need and you can then print a simple "10" string if needed.

Ori Lentz
  • 3,668
  • 6
  • 22
  • 28
0

If that is all you want to do you can do like this:

double number = 10.0;
String output = ("" + (int)number);
System.out.println(output);
brso05
  • 13,142
  • 2
  • 21
  • 40
0

The easiest way will be by doing this.

double d = 10.0
String str = String.valueOf(d);
System.out.println(str.substring(0,str.indexOf(".")));

Since we are dealing with double, rounding off can be done. Refer this SO

Community
  • 1
  • 1
aksappy
  • 3,400
  • 3
  • 23
  • 49
0

try this:

    double  d = 10; 

    System.out.println(new BigDecimal(d).setScale(0, RoundingMode.HALF_UP).toPlainString());

will print only 10 without trailing zeros, or you can change the scale if you wish.

The advantage over casting to int is that, there is rounding, and 9.9 will be rounded to 10, while cast to int will give 9.

Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32
0

Try this. I think it should work.

String s=String.valueOf(10.0);
s=s.replace(".0","");
System.out.println(s);
Prakhar
  • 710
  • 6
  • 24
0

You could compare with double 10.00 as in:

    final double TEN_DOUBLE = 10.00;
    double ten = 10.00;
    double nineAndSomething = 9.10;

    System.out.println("Formatted:" + ((ten == TEN_DOUBLE) ? "10" : ten));
    System.out.println("Formatted:" + ((nineAndSomething == TEN_DOUBLE) ? "10" : nineAndSomething));

This gives you the following output:

Formatted:10
Formatted:9.1

panagdu
  • 2,133
  • 1
  • 21
  • 36