Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check decimal and total length of number

Tags:

c#

regex

Using regex, I can check if it is decimal or not

^\d*\.?\d*$

But what I want to control is total length of those digit.

(^\d*\.?\d*$){1,10}

But I still cannot control it.

After 2 days later, my final solution is

(?=^\d*\.?\d*$)^.{1,10}$

Regular expression visualization

Debuggex Demo

like image 544
Frank Myat Thu Avatar asked May 26 '15 07:05

Frank Myat Thu


People also ask

What is the length of a decimal?

A DECIMAL(20,6) column has fourteen integer digits and six fractional digits. The integer digits require four bytes for nine of the digits and 3 bytes for the remaining five digits. The six fractional digits require 3 bytes. DECIMAL columns do not store a leading + character or - character or leading 0 digits.

How do you know how many decimal places a number has?

For the number of decimal places stated, count that number of digits to the right of the decimal and underline it. The next number to its right is called the 'rounder decider'.

How do you know which decimal is bigger or smaller?

The decimal number with the greater digit in the tenths place is greater. For example, 9.85 is greater than 9.65. If the digits in the tenths place are equal, compare the digits in the hundredths place. The decimal number with the greater digit in the hundredths place is greater.

How do you know which decimal is the largest?

The greater a decimal is, the closer it is to one whole. The smaller a decimal is the farther it is from one whole. The first thing you need to look at is the digit number in each decimal. These each have two digits in them, so you can compare them right away.


2 Answers

Here's a solution:

^(?!.{10})\d*\.?\d*$

This will check the whole number isn't more than 10 characters.

If you want to check the whole number isn't more than 10 digits (not counting the dot), you may use

^((?!.{11})\d*\.\d*|(?!.{10})\d*)$
like image 172
Denys Séguret Avatar answered Oct 03 '22 09:10

Denys Séguret


(^\d*\.?\d*$){1,10}
    ↑     ↑

The * means, zero or more. So your regex means "zero or more times, from 1 to 10 times", so {1,10} in this case is redundant. You need to remove * or apply {1,10} only to parts that doesn't have it.

For example, you can change your regex to:

^\d*(\.?\d{0,10})$

This matches numbers that has up to 10 digits after the dot. If you want to match a float number that has 10 digits in total, you can use negative lookahead (as @DenysSéguret already mentioned in his answer +1):

(?!.{10})\d*\.?\d*

It matches \d*\.?\d*$ only if it its length < 10 (including the dot).

like image 43
Maroun Avatar answered Oct 03 '22 09:10

Maroun