2

I have the method below, but for some reason it is not formatting the area to have only one decimal. All help is appreciated.

public double beraknaArea()
    {
       DecimalFormat formatter = new DecimalFormat("#0.0");
       double area = 0;
       area = radie*radie*3.14;
       formatter.format(area);
       return area;
     }

6 Answers 6

6

You are returning the same double, and not the output of the formatter.

The format() method does not alter the double that you pass in, it returns a String format of the double.

Sign up to request clarification or add additional context in comments.

2 Comments

@ametren Correct. If the poster wants a certain look to their double, they should really return a String in their method and then return formatter.format(area);.
Ya, sorry, I didn't intend that as a critique of your answer, just an observation.
2

No methods in Java may change the type of a declared variable, or mutate any primitive value (or indeed String). You need to return what formatter.format(area) returns, not the unchanged value of area.

public String beraknaArea()
{
   DecimalFormat formatter = new DecimalFormat("#0.0");
   double area = radie*radie*3.14;
   return formatter.format(area);
 }

4 Comments

Incredible, this is the only correct answer, and besides well-written, with a full code sample, and it got downvoted?? Sorry mate, but I can't give you more than one upvote. At least you're at zero now.
@MarkoTopolnik: not the downvoter, so I'm only guessing: perhaps the problem was exactly because the question was "homework", and in general it's better to just point solutions, explain, and not provide the full code sample immediately. Teach how to, not "do it".
@woliveirajr Yes, I see, that makes sense and I didn't notice the tag. Still, as OP already provided the full code and this answer only points out the single bit that is wrong with it, just in very clear terms, that DV was not deserved.
Given the elementary mistake of the question, I thought "showing" as well as "telling" would contribute to the "teaching", as there's clearly some fundamental misunderstanding somewhere.
1

Formatters format method return String

String temp = formatter.format(area);

System.out.println(temp);

Now you will see formatted code.

Comments

1

your statement formatter.format(area); is not modifying the variable area, the variable is still holding the same value as before, you may return a string if you want fromated output

Comments

0

The problem is that instead of returning the formatted area, you are returning the area which you computed, but which is not formatted.

Refer following:

public String beraknaArea()
    {
       DecimalFormat formatter = new DecimalFormat("#0.0");
       double area = 0;
       area = radie*radie*3.14;
       String formattedArea = formatter.format(area);
       return formattedArea;
     }



/************ OR **************/



public double beraknaArea()
    {
       DecimalFormat formatter = new DecimalFormat("#0.0");
       double area = 0;
       area = radie*radie*3.14;
       String formattedArea = formatter.format(area);
       return Double.parseDouble(formattedArea);
     }

The first solution returns the formattedArea as a String, whereas second one returns double value of formattedArea.

Comments

-1

You could multiply the number by 10, cast it as an integer, and then divide it by 10.0 to get a double with exactly one decimal point.

area = ((int) (area * 10)) / 10.0;

Instead of casting to an int, you could use Math.floor

area = Math.floor(area * 10) / 10.0;

Now, further arithmetic could make the number have a different number of decimal places.

Edit:

This is assuming the OP wants to keep the return value as a double instead of a String (as other answers have provided).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.