File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes NumberFormatException and NumberFormat Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "NumberFormatException and NumberFormat" Watch "NumberFormatException and NumberFormat" New topic
Author

NumberFormatException and NumberFormat

Sri Ram
Ranch Hand

Joined: Oct 03, 2005
Posts: 118
Hi Ranchers,
I was using NumberFormat for making the minimum integer digit to be 15. I encountered a NumberFormatException while converting back to Double.
Now my real Requirment is to enable some calculation for values more than 10 digits.
Here is the code. Where am i going wrong. Obviously the NumberFormat will give a value with like say "1,500,000,000,000,000" and when i convert this, i get NumberFormatException.

Now is there a method which will give me a Double value and also parsed value.

Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Double.valueOf is not expecting a String containing commas.

But I think you need to think more about what you are doing. A Format object, like one of type NumberFormat is there to parse strings into numbers or format numbers as string, or both.


There is no emoticon for what I am feeling!
Sri Ram
Ranch Hand

Joined: Oct 03, 2005
Posts: 118
Thanx Jeff, Still i am not able to figure out the solution.
Let me put it this way, I have a String which is a number of 15 Digits length. Now, I want to do some calculation on that, hence i am converting to a Double. Now it becomes 1.5E15. I cant do any calculation with this.
I want number as 1500000000000000. So what is that i have to do??
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Are you able to use BigDecimal or BigInteger?

Are you aware of the precision issues when using floating-point primitives?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Sri Ram
Ranch Hand

Joined: Oct 03, 2005
Posts: 118
Hey marc. But i cant use BigDecimal or BigInteger. This is cause it would involve Huge Change in application level.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Sri Ram:
...it becomes 1.5E15. I cant do any calculation with this.
I want number as 1500000000000000...

These are just different representations of the same quantity. Do your calculations. Then when you want to display a result, use the format method to give a nice-looking String representation.
Sri Ram
Ranch Hand

Joined: Oct 03, 2005
Posts: 118
Originally posted by marc weber:

These are just different representations of the same quantity. Do your calculations. Then when you want to display a result, use the format method to give a nice-looking String representation.


Marc, I am passing the value to a third party API which accepts String.
The loanAmount goes as "1.5E15|GHB" where as i have to send "1500000000000000|INR".

Now when the API parses that value, its giving me an Exception. Hence i have to pass the value the same way i enter it in UI.
Harshil Mehta
Ranch Hand

Joined: Mar 17, 2005
Posts: 64
Ram,


Marc, I am passing the value to a third party API which accepts String.
The loanAmount goes as "1.5E15|GHB" where as i have to send "1500000000000000|INR".


I don't think that the string "|GHB" in "1.5E15|GHB" is required. Is it so?
Sri Ram
Ranch Hand

Joined: Oct 03, 2005
Posts: 118
Originally posted by Harshil Mehta:
Ram,



I don't think that the string "|GHB" in "1.5E15|GHB" is required. Is it so?


U see i have to pass Loan Amount along with its Currency. Hence thats very much mandatory. Thats y i am getting this problem. If its just double or value, i could have passed it without any problem.


Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Two examples: each takes string "123456789012345", converts it to a number,
of some sort, doubles it and converts the result back to a string.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NumberFormatException and NumberFormat
 
Similar Threads
One big XML file or lots of small ones
double
Handling large numbers in javascript
How to avoid exponential in STRING to DOUBLE conversion
Convert String into double?