Hi Folks,
I have been searching to find a reglular expression that deals with decimal points, they are not mentioned on the Sun Tutorials for Regular Expressions. Basically im trying to verify that a string taken in from a screen matches the format of (15).99. This is not for my homework assignment:-) If anyone even knows of a good tutorial that covers this, that would be great as im kinda new to Java development.

Thanks

Man In A Can

Recommended Answers

All 7 Replies

1. check for the number of decimal points in the input. if this is not 1 => false
2. check that everything before the decimal point is a decimal. if not => false
3. check that everything after the decimal point is a decimal if not so=> false
4. check the number of decimals after the decimal pont. if not two => false

if all these checks cleared, you've got a match. don't know whether or not regex has a quick sollution for it, but you could as well do it without

Cool Thanks very much, i actually worked it out, i had been using "[0-9]{0,15}.{1}[0-9]{2}" and i have since found that this "[0-9]{0,15}[.]{1}[0-9]{2}" will work.

Thanks

Man In A Can

You can simplify it just a little more with a digit character match like so

\d{0,15}\.\d{0,2}

You might find this small regex testing form that I posted a while back helpful in tweaking patterns as you are experimenting: http://www.daniweb.com/forums/post392585-8.html
The numbered text fields at the bottom are the match group results of the pattern.

Thanks very much for all your help.

I would go with the following logic:

if no integer portion is provided, check if decimal is given along with atleast one fractinal portion of the decimal, also forces user to give atleast one digit
to avoid the empty string NumberFormatException when user provides ""
OR
if a single integer portion is provided, you may or may not give a fraction portion with or without a decimal "."
match the precision and scale first as a decimal number, pretty much solves avoiding NumberFormatException

if(Pattern.compile("^(\\d{0," + maxIntegralDigits + "}(\\.\\d{1," + maxFractionalDigits + "}))+|(\\d{1," + maxIntegralDigits + "}(\\.\\d{0," + maxFractionalDigits + "})?)$").matcher(fieldValue).matches()

which turns out to be

^(\\d{0,18}(\\.\\d{1,6}))+|(\\d{1,18}(\\.\\d{0,6})?)$

for an (18,6) (precision+scale, scale) representation

I would go with the following logic:

if no integer portion is provided, check if decimal is given along with atleast one fractinal portion of the decimal, also forces user to give atleast one digit
to avoid the empty string NumberFormatException when user provides ""
OR
if a single integer portion is provided, you may or may not give a fraction portion with or without a decimal "."
match the precision and scale first as a decimal number, pretty much solves avoiding NumberFormatException

if(Pattern.compile("^(\\d{0," + maxIntegralDigits + "}(\\.\\d{1," + maxFractionalDigits + "}))+|(\\d{1," + maxIntegralDigits + "}(\\.\\d{0," + maxFractionalDigits + "})?)$").matcher(fieldValue).matches()

which turns out to be

^(\\d{0,18}(\\.\\d{1,6}))+|(\\d{1,18}(\\.\\d{0,6})?)$

for an (18,6) (precision+scale, scale) representation

Atleast check the dates while posting, the last post on this thread is more than an year old !!!

Thank you Man In A Can. You saved my day

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.