I am trying to build a regex which will allow both negative and positive decimal numbers with the following rules.

- there can not be more than 2 digits after decimal point
- Decimal point is optional
- Total length including decimal point should not exceed 12 char
- if no decimal point is there, max length should not exceed 9 char

Can anyone help me out? thanks a lot in advance.

It is pretty straightforward since 12 - 9 == 3 for two decimals + the dot.

```
var re = new RegExp('^-?\\d{1,9}(\\.\\d{1,2})?$');
```

authorizes

`-123456789`

`-123456789.1`

`-123456789.12`

`0`

`0.12`

but will not accept

`01234567890123`

more than 12 decs`123.`

a dot without decimals`123.123`

more than two decimals`.`

or`.12`

(missing 0)

Check this regex.

```
^[+-]?[0-9]{1,9}(?:\.[0-9]{1,2})?$
```

This regex says

- sign is optional
- at least one and max 9 digits as integer part
- if decimal point is there, at least one and max two digits after it.

My own regex:

```
var rgx = /^(-{1}?(?:([0-9]{0,10}))|([0-9]{1})?(?:([0-9]{0,9})))?(?:\.([0-9]{0,3}))?$/;
```

Similar Questions

I am trying to write a regex for allowing 3 digit numbers followed by optional decimal, integer part cant be greater than 180 or smaller than -180. example of valid enteries are 156.56, 56.778, 9, 6.7

I am comparing lists of numbers and assigning scores for matching direction. Each match positive or negative should increase the participant's score by 1. For example: list1 =[5.6, -7.1, 6.4] list2

I created a classifier using libsvm toolbox in Matlab. It is classifying all positive class data as negative class and vice versa. I got good result while doing cross validation but while testing some

A table's boolean fields can be named using the positive vs the negative... for example, calling a field: ACTIVE , 1=on / 0=off or INACTIVE , 0=on / 1=off Question: Is there a proper way to mak

I need a program to convert Binary numbers into Decimal number in Java or in C++. is there some one who can help me.

I need a regular expression to validate a web form field that should contain an AS number in asdot notation as explained in RFC 5396: asdot refers to a syntax scheme of representing AS number values

My program should convert decimal numbers to binary. For big numbers it is giving me a negative number not a binary number. Why is this? For example, if I supply 2321 I get 100100010001, which is fine

I am trying to get my divisibility checker code to work correctly. It currently only works with whole positive numbers. If the number is a decimal or a negative number the code will not work. I know I

An interview question I was asked last week: I need a function to print out whether a number is positive or negative without using conditional statements like if else while for switch a? b:c etc. How

this is probably ridiculously simple but I'm not sure how to do this properly and couldn't find anything - if I have a positive decimal number, e.g 0.04, what's the best way to cast this to a negative

A function() that takes as input a list of positive numbers and a positive number target. My function returns True if there are three numbers in the list that add up to target. So far I've come up wit

Is there a functional difference between positive, negative, and neutral buttons, particularly in the context of AlertDialogs?

How to show edittext and listview and positive and negative button in dialogbox? I tried to show them separatly but i want all of these in one dialogbox. To show edit text i tried: AlertDialog.Builder

I have to validate using regex decimal number between 00.00 to 35.35 with following simple assumptions(I am using C#). 1) leading zeros are not required(optional). 2) 2 decimals are always required. I

While reading about printf(),I found that it can print numbers as positive or negative as desired by the user by following code(for -).But the code doesnt work and the output is a positive value.Pleas

How do you truncate all numbers after a decimal in Python 3? For example truncate 3.444 to be just 3.

I have a data set with two columns of positive and negative numbers. I would like to create a third column that reflects which quadrant they would appear in if plotted in Cartesian space. For example,

How do I make this program print out negative values along with positive values so there will be random numbers from -100 to 100? public class RandomInts { /* * This program makes 10 random integers a

My current regex format allow only number, including positive, negative, and floating decimal. regex: /^[\-\+]?(([0-9]+)([\.,]([0-9]+))?|([\.,]([0-9]+))?)$/, How to allow (add) dollar and comma in

I have old negative films I have scanned onto my computer. I want to write a small program to convert the negative Images to its positive state. I know there are several image editor applications out

I am working with a dataset, and will be returning both positive and negative currency figures. I already have a parenthesis around it. How do I get only the negative numbers in the gridview to show u

I know how computer today stores negative integers, which most of the computers use the 2' complement. I just wast wondering the 2' complement method applies for all kinds of numbers like floating poi

So I have this test program that checks the binary value of an integer one is positive and another is negative. int i = 100; int i2 = -100; System.out.println(Integer.toBinaryString(i)); System.out.pr

Is there any way to print the integer along with its sign in c++...i.e. by default if the number is negative we would get a - sign printed. In the same way can we get + before the positive numbers. in

How to convert float negative value to float positive value example -1.5 to 1.5

I need an algorithm to determine if a sentence, paragraph or article is negative or positive in tone... or better yet, how negative or positive. For instance: Jason is the worst SO user I have ever

In a If statement, how do I match a positive number with a negative one e.g. if 500 matches with -500, cut and paste One solution I thought of is changing one of the columns by *-1 before I do anythin

I want to avoid from decimal numbers in my Axis, how can I do that ? XAML: <Charting:Chart VerticalAlignment=Stretch HorizontalContentAlignment=Stretch> <Charting:Chart.Axes> <Cha

I was wondering whether there is a way to make a negative value, positive? Here is a snippet of my xslt document: <NumberOfLinesAtRate> <xsl:value-of select=number(/xsales:Qty) /> </N

I need a regular expression in javascript that will accept only positive numbers and decimals. This is what I have but something is wrong -- it doesn't seem to take single positive digits. /^[-]?[0-9

I want to truncate numbers (given as strings) to a fixed number of decimal places. The numbers can be negative (with a minus sign), positive (no sign). I'd prefer to round the numbers properly and kee

There is a very handy set of 2d geometry utilities here. The angleBetweenLines has a problem, though. The result is always positive. I need to detect both positive and negative angles, so if one line

I have a function which prevent users from entering negative values. Can anyone tell me what changes need to be made in the below regex to make this work. function testAmount(obj){ var reg0Str = '^[0-

I have the following regex: [\-+]?[0-9]*[02468]$ which matches positive or negative even numbers, but I also want it to not match '0'. How can I accomplish this? I can't find a way to translate Only

I would like to know if its possible to convert negative and positive strings to use an specific format, with a line of code. -1.80 into -18.00 or 1.80 into 18.00

I'm trying to format a column of numbers in rails - using number_to_currency. I want to display negative numbers with ()s - which is easy to do using the negative_format option. However, when I do thi

Would like to sort from largest positive to largest negative. Desired output: (3,2,1,0,-1,-2,-3), Current output: (3,2,1,0,-3,-2,-1) Initial array: [-0.87, 0.51, 3.34, 1.58, 2.67, 0.51,

I want to to check the sentence with regex that contain only negative lookahead. So if write ^(?!My Words)$ and Test string is some characters. I expect the regex should return true but it does not

I cant make my regex pattern to be able to input decimal or fractions this is my pattern: \\d{0,5}([./]\\d{0,3})? My Target input is for decimal(12345.123), fraction(12345 12/12) I also tried this pat

So I'm working through the C programming language book. And working through the calculator questions. It asked to add support for negative numbers; which I did but I found a weird behaviour I can't ex

I set usetext=True in matplotlib to use Latex for managing the font layout in my plot. Now the space between the x-axis and the xticklabel is different for positive and negative values as shown in the

trying to transfer one array with a combination of positive and negative numbers to a new array- but only where the elements are positive. This is what I have so far: $param = array(2, 3, 4, -2, -3, -

Possible Duplicate: Negative doubles or floats in Haskell (macports) I am new to haskell, and when I try to enter negative floating point numbers (e.g. -1.0) into ghci or use them in modules, I keep

I have a stacked bar chart like this: http://jsfiddle.net/XFX5T/ I would like to know if it's possible to have the total of the negative + positive values as the stacked label. if the total is positiv

I'm really puzzled by this. I'm dividing two positive numbers and getting a negative result (I'm using Java). long hour = 92233720368L / (3600 * 1000000 ); I got as result -132. But if I divide them

Hello i am trying to convert an array of negative value with positive value to just positive value. I have tried abs function but it didn't work. x = [10,9,8,7,6,5,4,3,2,1,0,-1,-2,-3,-4,-5,-6,-7,-8,-9

Regexes and I have a relationship of love and hate. I need to match (accept) all numbers except for number 1 and 0. Seeing it as math and not string, number >= 2 should be matched. Also please cons

Hi I've already written a DialogFragment. Now I've realized that i want it to have a positive and a negative button just like an AlertDialog. How can I achieve such a thing while maintaining the code

I am attempting to convert a negative angle (in degrees) to positive. But I am getting a compile error saying: test.cpp invalid operands of types 'double' and 'int' to binary 'operator%' test.cpp inv

How can I split a list of integers to 2 lists: one of positive numbers and the other of negative numbers ? this is my attempt: is it possible to do it ? split:: [Int]->[[Int]] split [] =[[]] split