Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

Formatting as Currency

Thread view: 
eric.steigerwald@va.gov - 27 Sep 2007 15:11 GMT
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
GArlington - 27 Sep 2007 15:48 GMT
On 27 Sep, 15:11, eric.steigerw...@va.gov wrote:
> Can anyone offer any help with this ? I have the following code :
>
[quoted text clipped - 13 lines]
>
> 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...
Lew - 27 Sep 2007 15:51 GMT
> On 27 Sep, 15:11, eric.steigerw...@va.gov wrote:
>> Can anyone offer any help with this ? I have the following code :
[quoted text clipped - 26 lines]
> 2) that means your mrate is defined as double.
> problem solved...

But not the fundamental problem.

Do not use double for monetary values.

Signature

Lew

GArlington - 27 Sep 2007 15:51 GMT
> On 27 Sep, 15:11, eric.steigerw...@va.gov wrote:
>
[quoted text clipped - 27 lines]
> 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
Andrew Thompson - 27 Sep 2007 15:56 GMT
>Can anyone offer any help with this ? I have the following code :
>
[quoted text clipped - 5 lines]
>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

Signature

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

eric.steigerwald@va.gov - 27 Sep 2007 16:10 GMT
> eric.steigerw...@va.gov wrote:
> >Can anyone offer any help with this ? I have the following code :
[quoted text clipped - 44 lines]
>
> Message posted via JavaKB.comhttp://www.javakb.com/Uwe/Forums.aspx/java-general/200709/1

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
Lew - 27 Sep 2007 16:51 GMT
> 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.

Signature

Lew

Andrew Thompson - 27 Sep 2007 16:59 GMT
>> 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.

Agreed.  I was so busy concentrating on the code
itself that it got to the point that I "could not see
the forest, for the trees".

Signature

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

Roedy Green - 27 Sep 2007 22:27 GMT
>double dollar2 = df.format(dollar);

DecimalFormat.format eats a double and produces a String.  You are
assuming it produce a double.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.