• Post Reply Bookmark Topic Watch Topic
  • New Topic

Validating a decimal number  RSS feed

 
Karan Jain
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
My requirement is to validate a decimal number. I have the information about the maximum integer digits and maximum allowed digits in the fraction part.

What can be the best way to do it?
I am unable to use DecimalFormat... Is there any way i can make use of set the interger and fraction part using setMaximumIntegerDigits and setMaximumFractionDigits of DecimalFormat and then parse my number and get a parse exception if the number is out of range.

Thanks in advance...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What was wrong with DecimalFormat? Have you looked at the BigDecimal(String) constructor? Those could tell you whether the string will parse to a number. If so, you can figure out how many digits are before & after any decimal point.

In Java 1.4 and later you could make a regular expression (or two or three) that test whether a string matches the format you need. See Pattern in the JavaDoc for more.
 
Karan Jain
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stan,
Thanks for the reply...

I was using parse for validating but figured out its the wrong way...
I am using JDK 1.3 so regex is not available.

In my validation i require 2 things:
1) Validate whether is the string is a number or not
2) The integer and fraction part is also as per my requirement.

I think i might use StringTokenizer too... What do you suggest?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!