Regexp for digit with decimal with maximum value - regex

I have a regexp to check for a decimal with 2 numbers, but I want to check both the integer and the decimal part for their length.
/^\s*-?[1-9]\d*(\.\d{1,2})?\s*$/;
The above code is decimal with length 2 (ex: 12.23) but I want 10 integer value and 2 decimal value (10,2) like,
1234567890.12

Use /^(?![.])\d{0,10}(\.\d{1,2})?$/
It allows 1.23, 1.2 0.2
Invalid values ., 1.

Depending on what you exactly want, you can use:
/^\s*-?(\d{1,10}(\.\d{1,2})?)\s*$/
for input like: 12.23, 3.4, 1234567890.34, 4, 456, etc., or:
/^\s*-?(\d{10}(\.\d{1,2})?)\s*$/
for: 9087654321, 1234567890.1, 1234567890.23 (10 digits, and optional point and one or two digits), or:
/^\s*-?(\d{10}\.\d{2})\s*$/
for exactly 10 digits fallowed by point and 2 digits, like: 9087654321.12, etc. Its all depends on what kind of numbers you want to filter.

Related

Regex to accept only numeric input with whole and half decimal point

I have used the below regex but it accept all values after decimal point. I want only whole numbers ( eg: 12) and half decimal point (eg 12.5)
Regex regex = new Regex("[^0-9.]+");
I want the below behavior.
For example
Valid numbers : 12, 12.5
Invalid numbers 12.1, 12.8
Try using this pattern:
\d+(?:\.5)?
This would match whole numbers, as well as numbers which half just a decimal component of 0.5. If you also want to allow for 0.0 decimal endings, then use:
\d+(?:\.[05])?
For your actual code, you may use:
Regex regex = new Regex("#\d+(?:\.5)?");

Regular expression for counting decimals

I need to validate a Textbox input which accepts decimal values. I require to count the number of digits entered after dot in the decimal number and restrict if the precision is more than 5.
Ex
Valid values
1.2
15.256
25.25486
Invalid
455.256485
^\d*(?:\.\d{1,5})?$
This should validate decimals with 5 after ..
^\d*(?:\.\d{1,5})$
Use this if it is striclty for decimals and not integers.

Regular expression for price validation

Need regular expression which have:
Maximum 8 digits before decimal(.) point
Maximum 4 digits after decimal point
Decimal point is optional
Maximum valid decimal is 8 digits before decimal and 4 digits after decimal
So 99999999.9999
The regular rexpression I have tried ^\d{0,8}[.]?\d{1,4}$ is failing for 123456789
and more than this. means it is taking more than 8 digits if decimal point is not available.
Tested here : http://regexpal.com/
Many many thanks in advance!
^\d{0,8}(\.\d{1,4})?$
You can make the entire decimal optional
You can try this:
^\d{1,8}(?:\.\d{1,4})?$
or
^[1-9]\d{0,7}(?:\.\d{1,4})?$
If you don't want to have a zero as first digit.
You can allow this if you want: (.1234)
^[1-9]\d{0,7}(?:\.\d{1,4})?|\.\d{1,4}$
Any of the above did not work for me.
Only this works for me
^([0-9]{0,2}((.)[0-9]{0,2}))$
This regex is working for most cases even negative prices,
(\-?\d+\.?\d{0,2})
Tested with the following,
9
9.97
37.97
132.97
-125.55
12.2
1000.00
10000.00
100000.00
1000000.00
401395011
If there is a price of $9.97, £9.97 or €9.97 it will validate 9.97 removing the symbol.
1-(\$+.[1-9])
2-(\£+.[1-9])
You can use this expression for complete price digits.
I'm using this:
^[1-9]\d{0,7}(\.\d{1-4})$
^ = the start of the string
[1-9] = at least the string has to begin with 1 number between 1 and 9
\d{0,7} = optional or max 7 times d (digit: a number between 0 and 9)
() = create a group like a substring
. = need a .
\d{1-4} = digit repited max 4 time
$ end of the string
For price validation we can not allow inputs with leading repeating zeros like 0012 etc.
My solution check for any cases. Also it allows maximum 2 decimal point after the dot.
^(?:0\.[0-9]{1,2}|[1-9]{1}[0-9]*(\.[0-9]{1,2})?|0)$

Replace everything but number and >actual< decimal points

I'm trying to get a regex working that will replace everything except for numbers and a decimal point (easy). The tricky part: the decimal point is optional but, if present, must be trailed by a further number.
So:
.10 => 10
10. => 10
10.- => 10
1.0 => 1.0
I'm not quite sure how to define the "except numbers followed by an optional decimal point but mandatory number after the optional decimal point" bit :)
Thanks!
It would be something like this:
\d+(\.\d+)?
(Please note that the regex syntax you are using may require different escaping.)

Extra leading zeros when printing float using printf?

I'd like to be able to write a time string that looks like this: 1:04:02.1 hours using printf.
When I try to write something like this:
printf("%d:%02d:%02.1f hours\n", 1, 4, 2.123456);
I get:
1:04:2.1 hours
Is it possible to add leading zeros to a float formatting?
With the %f format specifier, the "2" is treated as the minimum number of characters altogether, not the number of digits before the decimal dot. Thus you have to replace it with 4 to get two leading digits + the decimal point + one decimal digit.
printf("%d:%02d:%04.1f hours\n", 1, 4, 2.123456);
Try %04.1f instead of %02.1f. The "4" here means at least 4 characters will be printed, and "2.1" has 3 (> 2) characters, so to enable the padding zeros you need 4.