-6

Possible Duplicate:
How to round a number to n decimal places in Java

I am having a Double value like "1234.9678" . i want to return only with one decimal without rounding or ceiling.

I have used this way but its returning "1234.96"

public double roundTwoDecimals(double d) {

DecimalFormat twoDForm = new DecimalFormat("#.##");

return Double.valueOf(twoDForm.format(d)); }

I want only "1234.9",

I have even used this way... But no luck... Can anyone help?

double d = 1234.9678;
int decimalPlace = 1;
BigDecimal bd = new BigDecimal( Double.toString(d) );
bd = bd.setScale( decimalPlace, BigDecimal.ROUND_HALF_UP );
System.out.println( bd.doubleValue() );
Community
  • 1
  • 1
Sandy
  • 303
  • 7
  • 18
  • 9
    What do you mean "without rounding"? And if you want just one decimal, how about `new DecimalFormat("#.#")`? – Thilo Oct 17 '12 at 08:54
  • 2
    -1, don't think OP put any effort to solve the problem himself. – Reddy Oct 17 '12 at 08:57
  • 1
    wow 0 accept over 13 questions... – UmNyobe Oct 17 '12 at 08:57
  • It is returning 1234.0 i mean rounding the value to higher precision, but i want "1234.9" "without rounding" to next higher precision. – Sandy Oct 17 '12 at 08:57
  • 4
    @Thilo Why? Because we want SO to become a better repository of **answered** questions, so it can help others as well? Ok, then I am nuts... ^^ – brimborium Oct 17 '12 at 09:03
  • 2
    If you want no rounding, why are you specifying a rounding mode that does rounding? And please don't say things like 'no luck' here. The information is useless. Say what you got and what you expected. – user207421 Oct 17 '12 at 09:04
  • @brimboriumless-- If possible answer my question rather than commenting..which will be useful to some others when needed as Thilo mentioned.. – Sandy Oct 17 '12 at 09:07
  • 3
    @Sandy Your question was already nicely answered by EJP and berry, why should I add another answer with the same content? All you need to do now is to accept one of the answers (the one you think was most helpful to you) and everyone is happy. ;) – brimborium Oct 17 '12 at 09:11
  • 2
    @Sandy Welcome to Stack Overflow. There are two kinds of responses to questions: answers, and comments. The posts you refer to are comments, and they have their own value. – user207421 Oct 17 '12 at 09:11
  • @Reddy got the answer from berry120 which may be useful for you in future...Make use of it instead of just commenting... – Sandy Oct 17 '12 at 09:22
  • @brimborium already said what I want to say. You could have find a solution yourself if you put a little effort. – Reddy Oct 17 '12 at 09:34
  • 1
    @Sandy You got the *wrong* answer from berry120, as he has tacitly admitted himself by deleting it. Don't you read the other answers? or just the ones you like? – user207421 Oct 17 '12 at 09:43

1 Answers1

6
public double roundTwoDecimals(double d)

No method with this signature can possibly do what you ask. See here for counter-example.

Floating-point variables don't have decimal places, so they cannot be rounded to specific numbers of decimal places. They have binary places.

You have to use a decimal radix. In Java this means using either DecimalFormat to convert to a String, or BigDecimal to convert to a number in decimal radix, round in the decimal radix, then convert that to a String, which is also going to be in decimal radix.

And that implies that you have to change the signature of the method, to either:

public BigDecimal roundTwoDecimals(double d)

or:

public String roundTwoDecimals(double d)
Community
  • 1
  • 1
user207421
  • 289,834
  • 37
  • 266
  • 440