Formatting as Currency

E

eric.steigerwald

Can anyone offer any help with this ? I have the following code :

DecimalFormat df = new DecimalFormat("0.00");
mrate = show_student_awards.getString("monthly_rate");
double dollar = Double.parseDouble(mrate);
double dollar2 = df.format(dollar);

the code runs up to the point where I attempt to do the format. There
it returns an error saying that it expects a double, and found a
string. The data in the resultset (show_student_awards) is in the SQL
2005 database as a currency data type. I've also tried retrieving it
as a double, but get the same result. Java just won't seem to convert
it to a double.

Thanks.

Eric
 
G

GArlington

Can anyone offer any help with this ? I have the following code :

DecimalFormat df = new DecimalFormat("0.00");
mrate = show_student_awards.getString("monthly_rate");
double dollar = Double.parseDouble(mrate);
double dollar2 = df.format(dollar);

the code runs up to the point where I attempt to do the format. There
it returns an error saying that it expects a double, and found a
string. The data in the resultset (show_student_awards) is in the SQL
2005 database as a currency data type. I've also tried retrieving it
as a double, but get the same result. Java just won't seem to convert
it to a double.

Thanks.

Eric

I presume that you define mrate somewhere as String too. If so, there
is NO way the error is where you say it is...
Working backwards logically:
1) your error message "it returns an error saying that it expects a
double, and found a string" means that you are trying to assign string
to variable defined as double. The ONLY place where I can see you
doing that is on line mrate =
show_student_awards.getString("monthly_rate"); mrate here is not
defined, but I presume it is a string.
2) that means your mrate is defined as double.
problem solved...
 
L

Lew

GArlington said:
I presume that you define mrate somewhere as String too. If so, there
is NO way the error is where you say it is...
Working backwards logically:
1) your error message "it returns an error saying that it expects a
double, and found a string" means that you are trying to assign string
to variable defined as double. The ONLY place where I can see you
doing that is on line mrate =
show_student_awards.getString("monthly_rate"); mrate here is not
defined, but I presume it is a string.
2) that means your mrate is defined as double.
problem solved...

But not the fundamental problem.

Do not use double for monetary values.
 
G

GArlington

I presume that you define mrate somewhere as String too. If so, there
is NO way the error is where you say it is...
Working backwards logically:
1) your error message "it returns an error saying that it expects a
double, and found a string" means that you are trying to assign string
to variable defined as double. The ONLY place where I can see you
doing that is on line mrate =
show_student_awards.getString("monthly_rate"); mrate here is not
defined, but I presume it is a string.
2) that means your mrate is defined as double.
problem solved...

Sorry, correction...
<your code>
....
double dollar2 = df.format(dollar);
</your code>
I should have spotted that!!!

Please see what is returned by DecimalFormat.format()
http://java.sun.com/j2se/1.4.2/docs/api/java/text/DecimalFormat.html
 
A

Andrew Thompson

Can anyone offer any help with this ? I have the following code :

DecimalFormat df = new DecimalFormat("0.00");
mrate = show_student_awards.getString("monthly_rate");
double dollar = Double.parseDouble(mrate);
double dollar2 = df.format(dollar);

the code runs up to the point where I attempt to do the format. There
it returns an error saying that it expects a double, ..

Please always copy/paste errors and exceptions.
It also helps to create an SSCCE* that others can
use to compare the code and any errors.

* <http://www.physci.org/codes/sscce.html>


Here is an example of an SSCCE.

<sscce>
import java.text.DecimalFormat;

class TestDecimalFormat {
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("0.00");
String mrate = "4987.56";
double dollar = Double.parseDouble(mrate);
//double dollar2 = df.format(dollar);
String dollar2 = df.format(dollar);
System.out.println("Your bill is: $" + dollar2);
}
}
</sscce>

I'll leave it at that, but note that it is far better
to use the inbuilt power of Java to to some things,
and formatting currency is one of them.

Look into NumberFormat.getCurrencyInstance(Locale)
Sun pays its engineers big bucks to figure these
things out for us.

HTH

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200709/1
 
E

eric.steigerwald

Can anyone offer any help with this ? I have the following code :
DecimalFormat df = new DecimalFormat("0.00");
mrate = show_student_awards.getString("monthly_rate");
double dollar = Double.parseDouble(mrate);
double dollar2 = df.format(dollar);
the code runs up to the point where I attempt to do the format. There
it returns an error saying that it expects a double, ..

Please always copy/paste errors and exceptions.
It also helps to create an SSCCE* that others can
use to compare the code and any errors.

* <http://www.physci.org/codes/sscce.html>

Here is an example of an SSCCE.

<sscce>
import java.text.DecimalFormat;

class TestDecimalFormat {
public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("0.00");
String mrate = "4987.56";
double dollar = Double.parseDouble(mrate);
//double dollar2 = df.format(dollar);
String dollar2 = df.format(dollar);
System.out.println("Your bill is: $" + dollar2);
}}

</sscce>

I'll leave it at that, but note that it is far better
to use the inbuilt power of Java to to some things,
and formatting currency is one of them.

Look into NumberFormat.getCurrencyInstance(Locale)
Sun pays its engineers big bucks to figure these
things out for us.

HTH

Thanks for all the help. Problem turned out to be the (dumb) mistake
of trying to use a double as the return of the format function,
instead of a string.

Eric
 
L

Lew

Thanks for all the help. Problem turned out to be the (dumb) mistake
of trying to use a double as the return of the format function,
instead of a string.

Actually, the problem is in the use of double to represent monetary values.
Don't do that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top