• Post Reply Bookmark Topic Watch Topic
  • New Topic

NumberFormatException and NumberFormat  RSS feed

 
Sri Ram
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sri Ram
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you able to use BigDecimal or BigInteger?

Are you aware of the precision issues when using floating-point primitives?
 
Sri Ram
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey marc. But i cant use BigDecimal or BigInteger. This is cause it would involve Huge Change in application level.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two examples: each takes string "123456789012345", converts it to a number,
of some sort, doubles it and converts the result back to a string.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!