1

For example, when I have 3.333 and when that code is executed, I need the number to be 3.33.

This is the code I have which should be doing that, but doesn't:

String number = Double.toString(first);
number = number.substring(0, string.length() - 1);
first = Double.parseDouble(number);

I tried doing it by converting the number to a string, cutting off last character and saving it into a double again. But it doesn't work. Instead of cutting off 1 digit, it cuts off 2, for example above it would return 3.3.

Is this method reliable and if yes, what can I do to fix it?

Also, is there a chance for this method to crash the program (only decimal numbers go through that code) and would there be a loss in precision?

Leonz
  • 177
  • 3
  • 14
  • I suggest using BigDecimal instead of double – scarecrow- Jun 13 '16 at 14:53
  • 1
    why? i see no reason for it – loonytune Jun 13 '16 at 14:53
  • 3
    String has a formatter to format the string to the precision you want. You'll have to print it in a string to get the precision you want. But don't convert to string then back to double. Here is the [API for String.Format](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format(java.lang.String,%20java.lang.Object...)) – Frecklefoot Jun 13 '16 at 14:56
  • Possible Duplicate: http://stackoverflow.com/questions/2808535/round-a-double-to-2-decimal-places – Dean Meehan Jun 13 '16 at 14:56
  • @Frecklefoot I'll look into it now. And I guess there is no way to crash the program by using that way? – Leonz Jun 13 '16 at 14:58
  • @DeanMeehan that question is about rounding to a certain precision every time. My question is about only cutting off the last digit. – Leonz Jun 13 '16 at 14:59

6 Answers6

5

Use String.format:

String.format("%.2f", 3.333);

Or use BigDecimal:

new BigDecimal(3.333).setScale(2, RoundingMode.DOWN)

setScale sets how many decimal points you need. You can later do a toString() on the above BigDecimal and print it out.

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
  • The BigDecimal method is for me a very good option. To transform to double just use the BigDecimal.doubleValue() method – Facepalmed Jun 19 '16 at 11:38
2

What about this:

double value = 3.333;
// Multiply it by 100, convert the result into an integer to trim 
// remaining decimals then divide it by 100d to get the result as double
double result = (int)(value * 100) / 100d;
System.out.println(result);

Output:

3.33

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
1

If you want always the same number of decimals the best solution as far as i know is this one:

DecimalFormat df = new DecimalFormat("#.00");

String formatedNumber = df.format(number);
SCouto
  • 7,808
  • 5
  • 32
  • 49
1

try this:

double d = 3.333;
String str = String.format("%1.2f", d);
d = Double.valueOf(str);
System.out.println(d);

output:

3.33

Seek Addo
  • 1,871
  • 2
  • 18
  • 30
-1

The main problem with what you're trying to accomplish is that Doubles are always 64 bits of information. They are supposed to be as accurate as possible while representing abstract/irrational values and not intended to be manipulated in terms of that accuracy. They can't be trusted for exact values (the input value 1 might end up as 0.99999999998 when saved as a double or something to that effect). If you want an exact number, consider using ints in some capacity (ie you could make a fraction object that holds a numerator and denominator). The only time you should cut down a double is for printing, and that can only really be done with a String representation as you did above.

Liam Duncan
  • 124
  • 1
  • 3
  • He's not asking about rounding, just cutting off last digits. `String.format()` can handle this task and is provided _precisely_ for this type of problem. – Frecklefoot Jun 13 '16 at 15:05
  • Their String rounding technique is fine... this isn't a String problem at all it's a value problem as in there's a fundamental misunderstanding of floating point numbers. They're using `Double.parseDouble()` and hoping to get a number rounded to one decimal place. Why would you downvote my answer without reading the question?? There's no right answer to this question, it's just a mislead question and an opportunity to learn. – Liam Duncan Jun 13 '16 at 15:11
  • I'll take your "there's no one right answer" response to heart. But I do think you made it more complicated than it needed to be. But, I guess your solution may work, but I wouldn't approach it that way. – Frecklefoot Jun 13 '16 at 15:15
  • That's fair, it might be complicated, but if it's the real truth (which I'm fairly sure it is) then it might be the best thing for OP to hear especially if they're at a fairly basic level which seems to be the case. – Liam Duncan Jun 13 '16 at 15:20
  • To build a better foundation going forward. – Liam Duncan Jun 13 '16 at 15:22
  • Sure. I would undownvote your answer if possible, but SO doesn't allow it. – Frecklefoot Jun 13 '16 at 15:23
  • huh that's a shame. Oh well I lost like 2 rep nbd – Liam Duncan Jun 13 '16 at 16:57
  • hopefully OP reads it if it's of any use even though it's dv'd – Liam Duncan Jun 13 '16 at 16:57
-1

This code is reliable.

Double number = 3.333 ;
String  mano   = Double.toString(number);
String akmuo = mano.substring(0, mano.length() - 1);
number = Double.parseDouble(akmuo);
System.out.println(number);
David Buck
  • 3,752
  • 35
  • 31
  • 35