Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

number from String

 
Joe Wolf
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given a String, is there a way to make sure that it's a valid real number, containing only the digits 0 - 9 and has no more than one decimal point? Would attempting a cast and catching an exception work or is there a better way?
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the API for parseInt(). There might be a parseFloat() or parseDouble()
------------------
Bosun
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use valueOf(), pass the string to it. If it ain't a number that will fit within the wrapper object you're using, it will throw a NumberFormatException.
If you wanted, you could use a bunch of if statements or something like that, but I would imagine the best way would be using a try/catch( NumberFormatException e ) block
[This message has been edited by jason adam (edited August 16, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic