• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Validating a decimal number

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic