-3

Pretty simple question I imagine, just can't seem to figure out the right way to do it.

I have a method that produces my total "cost" (a pre-defined 'double' variable) as a dollar amount here:

import java.text.NumberFormat;

public class Example { 

private double cost = 3.5;

NumberFormat cash = NumberFormat.getCurrencyInstance();

public String getTotal(){
    String money = cash.format(cost);
    return money;
    }
}

This works & prints "$3.50" correctly.

I was wondering if it's possible to do something along the lines of:

public double getTotal(){
    return cash.format(cost);
}

or

public double getTotal(){
    double money = cash.format(cost);
    return money;
}

Obviously the above doesn't compile, "cannot convert from String to double". Can I initiate the NumberFormat as a double instead of a String?

Seems completely unnecessary to me, this is the UML i'm trying to follow

+getTotal : double
7
  • 1
    Please provide an minimal reproducible example Commented Mar 17, 2017 at 7:55
  • Try to copy this code to an IDE and run it. You will see it doesn't work, so it is not a complete example Commented Mar 17, 2017 at 7:57
  • Show at Minimum the declaration and definiton of cost and the result you expected and the definition of cash. Commented Mar 17, 2017 at 8:03
  • if you give us the necessary Information i will remove my downvote. So edit your question and add the Information. Commented Mar 17, 2017 at 8:09
  • Is that better? Commented Mar 17, 2017 at 8:15

2 Answers 2

1

To change a string value to a double, One can use

String text = "12.34";
Double.parseDouble(text);

You can use this in your function to get double value

public double getTotalCost(){
   return Double.parseDouble(cash.format(cost));
}
Sign up to request clarification or add additional context in comments.

8 Comments

Why you should Format a double to a string and than parse it as a double? That makes no sence
@Jens where is the double to string conversion is done here?
cost is a double (a pre-defined 'double' variable) and format returns a string
The op has asked for it, so I provided the code. We are here to answer what has been asked and not DownVote evrything based on personal issues and beliefs. He has mentioned "just trying to gain knowledge in order to follow the UML for an assignment", so I provided him with the knowledge!
@Jens Then why would he write his function as public double getTotalCost(){ return cash.format(cost); }
|
0

cash.format() Returns a string, so your method segnature must be:

public String getTotalCostAsString(){
    return cash.format(cost);
}

You can not use public double, becaue $3.50 is not a double. A double is a numerical value without a curreny sign.

4 Comments

I understand that the way I have it needs to be returned as a string, the UML says +getTotalCost : double
@BCarmar I do not know it. Mybe ot should only return a value with two decimals but without currency sign?
@BCarmar if you Need the rounded value see here: stackoverflow.com/questions/2808535/…
Thanks for the feedback, I'll try messing with decimalformat

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.