-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
takendarkk
  • 3,347
  • 8
  • 25
  • 37
BCarmar
  • 11
  • 7

2 Answers2

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));
}
san A
  • 177
  • 2
  • 13
  • Why you should Format a double to a string and than parse it as a double? That makes no sence – Jens Mar 17 '17 at 08:01
  • @Jens where is the double to string conversion is done here? – MSD Mar 17 '17 at 08:04
  • cost is a double `(a pre-defined 'double' variable)` and format returns a string – Jens Mar 17 '17 at 08:06
  • 2
    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! – san A Mar 17 '17 at 08:10
  • @sanA and why not `return cost;`? – Jens Mar 17 '17 at 08:15
  • @Jens maybe he wanted to know how to change a string into a double. Ofcourse returning cost is correct. – san A Mar 17 '17 at 08:16
  • I do not think so. I think he has to return a string not a double – Jens Mar 17 '17 at 08:18
  • 1
    @Jens Then why would he write his function as `public double getTotalCost(){ return cash.format(cost); }` – san A Mar 17 '17 at 08:25
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.

Jens
  • 67,715
  • 15
  • 98
  • 113
  • I understand that the way I have it needs to be returned as a string, the UML says +getTotalCost : double – BCarmar Mar 17 '17 at 08:25
  • @BCarmar I do not know it. Mybe ot should only return a value with two decimals but without currency sign? – Jens Mar 17 '17 at 08:29
  • @BCarmar if you Need the rounded value see here: http://stackoverflow.com/questions/2808535/round-a-double-to-2-decimal-places – Jens Mar 17 '17 at 08:40
  • Thanks for the feedback, I'll try messing with decimalformat – BCarmar Mar 17 '17 at 08:44