I have a regular expression:
/^(([1-9]+\.[0-9]*)|([1-9]*\.[0-9]+)|([1-9]+))([eE])([-+]?[0-9]+)?$/
to validate exponential value.it is working fine.But some values like 21E,31E it is considering as exponential value but values like 09E it is considering non exponential value.
Can we have the solution for this ,So that it ll consider all values like 21E,31E,09E as non-exponential value.
Thanks
A mantissa is either a 0, or a nonzero digit possibly followed by digits.
0|([1-9][0-9]*)
A fractional part is a possibly empty string of digits.
[0-9]*
A real number is a mantissa followed by a point followed by a fractional part.
(0|([1-9][0-9]*))\.[0-9]*
An exponent is e followed by a mantissa.
(eE)(0|([1-9][0-9]*))
A scientific number is a real number optionally followed by an exponent
(0|([1-9][0-9]*))\.[0-9]*([eE](0|([1-9][0-9]*)))?
Some variants are possible, as the OP didn't give a complete specification.
Related
For example:
var a=x.yz;
var b=-x.yz;
after they round to the "error" value, would -a exactly equal to b?
Or would it round to different values like Math.round(2.5) and Math.round(-2.5)?
If the numeral has no more than 20 significant decimal digits, the results of converting a numeral with a - and a numeral without a - are exactly the same except for the sign.
JavaScript is an implementation of ECMAScript, specified in Ecma-262 and ISO/IEC 16262. Ecma-262 specifies that the IEEE-754 64-bit binary floating point format is used, except there is a single NaN value.
Clause 7.1.3.1 specifies how a String (containing a numeral, such as 1.2345) is converted to a Number. Unless the numeral is a decimal numeral with more than 20 significant digits, it is converted as specified in clause 6.1.6, which specifies rounding rules corresponding to IEEE-754’s round-to-nearest-ties-to-even method. That method is symmetric with respect to sign, and therefore the result of converting the negation of a numeral to a JavaScript Number equals the negation of the result of converting the numeral to a Number.
If the numeral has more than 20 significant decimal digits, clause 7.1.3.1 allows an implementation to use either of the two 20-digit numerals nearest the source numeral. The document does not impose further requirements on this choice, so a JavaScript implementation could behave asymmetrically with respect to sign in this case. However, it would be a poor implementation choice to do so.
https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#round-float-
As documentation says Math.random( float ) will round to positive infinity, this will help you to understand why Math.round(2.5) and Math.round(-2.5) returns a different value. So in your case -(-a) will be rounded in fact as +a.
Since integers above 2^53 can't be accurately represented in doubles, how does JS decide on their decimal representation when they are printed as strings?
For example, 2^55 is 36028797018963968, and printf("%lf",(double)(1LL<<55)) in C will print that number correctly, since it has trailing zeroes in its binary representation that do not cause precision loss when truncated.
However, in Javascript, we get 36028797018963970 instead. It seems to try to round numbers to get a 0 at the end, but not always - for instance, 2^55-4 is represented correctly with 4 at the end.
Is there some place in the spec that defines this weird behavior?
Question- Since integers above 2^53 can't be accurately represented in doubles, how does JS decide on their decimal representation when they are printed as strings?
1. Way of Printing decimal numbers in JS
JavaScript numbers are internally stored in binary floating point and usually displayed in the decimal system.
There are two decimal notations used by JavaScript:
Fixed notation
[ "+" | "-" ] digit+ [ "." digit+ ]
Exponential notation
[ "+" | "-" ] digit [ "." digit+ ] "e" [ "+" | "-" ] digit+
An example of exponential notation is 1.2345678901234568e+21.
Rules for Displaying decimal numbers:
A. Use exponential notation if there are more than 21 digits before the decimal point.
B. Use exponential notation if the number starts with “0.” followed by more than five zeros.
2. The ECMAScript 5.1 display algorithm
Here is a details of Sect. 9.8.1 of the ECMAScript 5.1 specification describes the algorithm for displaying a decimal number
Given a number
mantissa × 10^pointPos−digitCount
The mantissa of a floating point number is an integer – the significant digits plus a sign. Leading and trailing zeros are discarded. Examples:
The mantissa of 12.34 is 1234.
Case-1. No decimal point: digitCount ≤ pointPos ≤ 21
Print the digits (without leading zeros), followed by pointPos−digitCount zeros.
Case-2. Decimal point inside the mantissa: 0 < pointPos ≤ 21, pointPos < digitCount
Display the pointPos first digits of the mantissa, a point and then the remaining digitCount−pointPos digits.
Case-3. Decimal point comes before the mantissa: −6 < pointPos ≤ 0
Display a 0 followed by a point, −pointPos zeros and the mantissa.
Case-4. Exponential notation: pointPos ≤ -6 or pointPos > 21
Display the first digit of the mantissa. If there are more digits then display a point and the remaining digits. Next, display the character e and a plus or minus sign (depending on the sign of pointPos−1), followed by the absolute value of pointPos−1. Therefore, the result looks as follows.
mantissa0 [ "." mantissa1..digitCount ]
"e" signChar(pointPos−1) abs(pointPos−1)
Question-
However, in Javascript, we get 36028797018963970 instead. It seems to try to round numbers to get a 0 at the end, but not always - for instance, 2^55-4 is represented correctly with 4 at the end.
Is there some place in the spec that defines this weird behavior?
Check: How numbers are encoded in JavaScript specially ==>5. The maximum integer
Additional Reference: https://medium.com/dailyjs/javascripts-number-type-8d59199db1b6
I have a regex
^(?=.*[1-9])\\d{0,5}(?:\\.\\d{0,2})?$
to check amount should be greater then 0 and it can contain maximum five digit before decimal and optional decimal and 2 digits after decimal.
Here (?=.*[1-9]) is to check there should be latest one occurrence of any digit from (1-9) in the string.
But I want modify it to check amount greater then one(1), so I want (?=.*[1-9]) to check only till the occurrence of the decimal point i.e. 0.1 it should return false.
Other condition should also fulfill.
Note : the count of digits before decimal is not fix, it will very from 1 to 5, so we can't modify it to (?=.{0,5}[1-9])
You can fix it by using this negation based regex:
^(?=[^.]*[1-9])\\d{0,5}(?:\\.\\d{0,2})?$
RegEx Demo
[^.]* before [1-9] will match any character except decimal point thus not allowing 0.45 as valid number.
I need to validate a numeric string with JavaScript, to ensure the number has exactly two decimal places.
The validation will pass only if
the number has precisely two decimal places
there is at least one digit before the decimal point. (could be zero)
the number before the decimal point can not begin with more than one zero.
Valid numbers:
0.01
0.12
111.23
1234.56
012345.67
123.00
0.00
Invalid numbers:
.12
1.1
0.0
00.00
1234.
1234.567
1234
00123.45
abcd.12
12a4.56
1234.5A
I have tried the regular expression [0-9][\.][0-9][0-9]$, but it allows letters before decimal point like 12a4.56.
. matches any character, it does not do what you think it does. You have to escape it. Also, you have two more errors; try
^[0-9]+\.[0-9][0-9]$
instead, or even better, use \d for decimal digits:
^\d+\.\d\d$
This covers all requirements:
^(0|0?[1-9]\d*)\.\d\d$
the number has precisely two decimal places
Trivially satisfied due to the non-optional \.\d\d$
The other two conditions can be restated as follows:
The number before the decimal points is either a zero
or a number with exactly one zero, then a number that does not start with zero
This is covered in these two cases:
0
0?[1-9]\d*
You don't need regular expressions for this.
JavaScript has a function toFixed() that will do what you need.
var fixedtotwodecimals = floatvalue.toFixed(2);
i used this
^[1-9][1-9]*[.]?[1-9]{0,2}$
0 not accept
123.12 accept but 123.123 not accept
1 accept
12213123 accept
sdfsf not accept
15.12 accept
15#12 not accept
15&12 not accept
var values='0.12';
document.write(values.match(/\d+[.]+\d+\d/));
change value as you want and check it
Here it is:
^(0[.]+\d{2})|^[1-9]\d+[.]+\d{2}$
Try This Code
pattern="[0-9]*(\.?[0-9]{1,2}$)?"
1 Valid
1.1 Valid
1.12 Valid
1.123 not Valid
only number Valid
pattern="[0-9]*(.?[0-9]{2}$)?"
1 Valid
1.1 not Valid
1.12 Valid
1.123 not Valid
only number Valid
I have written a Regex which will take only integers but I need to rewrite this regex to only allow 3 digit before precision and 2 digits after precision
How to do that?
If it must always have 3 digits, a decimal point, and 2 digits, e.g., 412.88, then:
/^\d{3}\.\d{2}$/
If it can be up to 3 digits before and up to 2 after (possibly no decimal point at all) then maybe something like:
/^\d{1,3}(\.\d{1,2})?$/
In c#
#"^\d{3}\.\d{2}$"
//in c# we need to use verbatim string `#""` to treat escape sequences as normal literals instead of giving it a special meaning..
In javascript
/^\d{3}\.\d{2}$/