CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2010
    Posts
    9

    converting a double to currency value

    All,

    I have an issue which I need some help on. I have a web app which the user input an amount into a text field (in this format: 00.00) which is passed to the form as a string. I use double.parse double so I can assign it to a currency value. I have posted some code which doesn't give me any warning:

    double partialAmount = Double.parseDouble(reversePartialPayment
    .getPartialAmount().trim());

    currency.aQuantity = partialAmount;

    aQuantity is a double as well. Here is my issue currency.aQuantity is passed to another class which is looking for currency and not a double. Is there a way to convert/cast the double to currency or another way around the problem? Everything I have read said using doubles for currency is a bad idea. Since I have the number formatted the way I want it, my thinking was it shouldn't be a problem. If I'm heading the wrong direction with the double cast, please point me in the right direction. Thanks in advance.

  2. #2
    Join Date
    Apr 2007
    Posts
    442

    Re: converting a double to currency value

    Frankly I do not understand your predicament. Using double for currency is not a good idea, when you could expect singular entries to be in around the Uncle Scrooge amounts. However, cannot bring myself to understand why to expect troubles in a two decimal setting.

    You talk of passing the double to another class, that looks for currency, and casting the double to currency etc. What in this context is the "currency", apparently not the Java Currency -class? But since, only input that you reveal to us, is just the primitive double, how in the world do you go about "looking for currency" in a primitive value? Simple double dont have that fancy hairdos.

    And basically, you cannot cast a primitive type to any actual class, save for a suitable wrapper class (such as Number, Double etc.). Without knowing more of the currency you are referring to, whats it like, how is it used etc. I cannot say if you can cast a double to that. However, it is safe to assume, that would seldom be a good approach.

    As such, provided the text passed to Double.parseDouble(...) is working, you do get a double. Else you get an exception. So, post again, include the problem.

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: converting a double to currency value

    Quote Originally Posted by Tony1427 View Post
    I use double.parse double so I can assign it to a currency value. I have posted some code which doesn't give me any warning:

    double partialAmount = Double.parseDouble(reversePartialPayment
    .getPartialAmount().trim());

    currency.aQuantity = partialAmount;

    aQuantity is a double as well. Here is my issue currency.aQuantity is passed to another class which is looking for currency and not a double.
    So why not pass the currency object instead of the double inside it?

    Is there a way to convert/cast the double to currency or another way around the problem?
    You just said you used a double so that you could assign it to a currency value - why decide to use a double if you don't know how to do the assignment?

    Everything I have read said using doubles for currency is a bad idea. Since I have the number formatted the way I want it, my thinking was it shouldn't be a problem.
    So you ignored everything you read because you didn't think it would be a problem doing what you don't know how to do

    Why then should we have any confidence you'll take advice offered here?

    All truths are easy to understand once they are discovered; the point is to discover them...
    G. Galilie
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured