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.
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.
Re: converting a double to currency value
Quote:
Originally Posted by
Tony1427
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?
Quote:
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?
Quote:
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 :confused:
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