`parseInt` is not working as expected when special characters exists - javascript

i have var value="10+10" when i try to convert this using parseInt(value) to an int it is giving me NaN. Is their any option to convert a string if it has special characters in it?
The Result shoud be 20 or simply 10+10

you can use eval to evaluate string operations.
since parseInt doesn't recognize characters like + it will return the numbers until special characters.
as a example
(parseInt("10+10") print 10 and
(parseInt("100+10") print 100 and
console.log(parseInt("10+10"))
console.log(parseInt("100+10"))
console.log(eval("10+10"))
console.log(eval("10*10"))

Related

How to get the correct element from a unicode string?

I want to get specific letters from an unicode string using index. However, it doesn't work as expected.
Example:
var handwriting = `𝖆𝖇𝖈𝖉𝖊𝖋𝖌𝖍𝖎𝖏𝖐𝖑𝖒𝖓𝖔𝖕𝖖𝖗𝖘𝖙𝖚𝖛𝖜𝖝𝖞𝖟𝕬𝕭𝕮𝕯𝕰𝕱𝕲𝕳𝕴𝕵𝕶𝕷𝕸𝕹𝕺𝕻𝕼𝕽𝕾𝕿𝖀𝖁𝖂𝖃𝖄𝖅1234567890`
var normal = `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`
console.log(normal[3]) // gives 'd' but
console.log(handwriting[3]) // gives '�' instead of '𝖉'
also length doesn't work as expected normal.length gives correct value as 62 but handwriting.length gives 114.
Indexing doesn't work as expected. How can I access the elements of unicode array?
I tried this on python it works perfectly but in Javascript it is not working.
I need exact characters from the unicode string like an expected output of 'd' '𝖉' for index 3
In Javascript, a string is a sequence of 16-bit code points. Since these characters are encoded above the Basic Multilingual Plane, it means that they are represented by a pair of code points, also known as a surrogate pair.
Reference
Unicode number of 𝖆 is U+1D586. And 0x1D586 is greater than 0xFFFF (2^16). So, 𝖆 is represented by a pair of code points, also known as a surrogate pair
console.log("𝖆".length)
console.log("𝖆" === "\uD835\uDD86")
One way is to create an array of characters using the spread syntax or Array.from() and then get the index you need
var handwriting = `𝖆𝖇𝖈𝖉𝖊𝖋𝖌𝖍𝖎𝖏𝖐𝖑𝖒𝖓𝖔𝖕𝖖𝖗𝖘𝖙𝖚𝖛𝖜𝖝𝖞𝖟𝕬𝕭𝕮𝕯𝕰𝕱𝕲𝕳𝕴𝕵𝕶𝕷𝕸𝕹𝕺𝕻𝕼𝕽𝕾𝕿𝖀𝖁𝖂𝖃𝖄𝖅1234567890`
console.log([...handwriting][3])
console.log(Array.from(handwriting)[3])
A unicode character looks like '\u00E9' so if your string is longer this is normal.
To have the real length of a unicode string, you have to convert it to an array :
let charArray = [...handwriting]
console.log(charArray.length) //=62
Each item of your array is a char of your string.
charArray[3] will return you the unicode char corresponding to '𝖉'

JavaScript conversion from String with "," to Int

How to convert 1,000 to 1000 using JavaScript.
console.log(parseInt(1,000));
is taking it as 1
You should replace the "," and then doo the parseInt
parseInt("1,000".replace(/,/g,""));
You need to replace comma with empty character. Something like this below:
parseInt("1,000".replace(/,/g, ""))
Try this,
var num = parseInt("1,000".replace(/\,/g, ''), 10);
As, we need to remove "comma" from the string.
We also need "10" as radix as the second parameter.
Thanks
You can use a regular expression to replace all non-digits except for - and . when passing the argument to parseInt:
function parseMyInt(str) {
return parseInt(str.replace(/[^-\.\d]/g,''));
}
console.log(parseMyInt("1,000"));
console.log(parseMyInt("1,000,000"));
console.log(parseMyInt("1,000.1234"));
console.log(parseMyInt("-1,000"));
Edit
Expanded the regex to account for negative numbers and decimals.

Determine if a string is only numbers or a comma

How can I determine if a string contains only numbers or a comma?
var x = '99,999,999';
var isMatch = x.match('/[\d|,]/');
The above always returns null.
To be a little more exact:
/^\d{1,3}(?:,\d{3})*$/
Only matches when the number is delimited by a comma after every 3rd digit from the right. (This doesn't account for decimals)
Try this: /^[\d,]+$/ Note that this will not tell you if the number is formatted correctly (i.e. ,99, will be accepted just as 99 or 9,999).
/^(?:\d{1,3}(?:,\d{3})*$|^\d+$)/ while more complex, this regex will test to see if the number is a properly formatted number (it won't accept ,,1,,,1,1111,,1,1,1 only 1,11,111,1,111,1111 etc.)

Regular expression for validating decimal numbers

function validateDecimal(number,maxDigits)
{
eval("var stringvar=/^[-+]?([0-9]*\.[0-9]{0,"+maxDigits+"})|([0-9]+)$/");
return stringvar.test(number);
}
I wrote above function to validate decimal numbers. The variable 'maxDigits' uses to specify the number of digits in fractional part and 'number' as the value to be validated. But it returned 'true' when I tried with a numeric value followed by a character for eg: 24y. Can anyone help me to figure out my mistake.
Without going into the regex, I think the problem in your code is that you should escape the special character twice. Since you're putting it all inside a string, a single backslash is escaped by the string parsing.
I think this should work:
eval("var stringvar=/^[-+]?([0-9]*\\.[0-9]{0,"+maxDigits+"})|([0-9]+)$/");
This regular expression would validate a number with maxDigits of decimals:
^[-+]?[0-9]*.[0-9]{10}$. This will validate it to 10 decimal places.
Implementing that into JavaScript would look like:
eval("var stringvar=^[-+]?[0-9]*.[0-9]{" + maxDigits + "}$");, or thereabouts.
I have just tried this one and it worked for me: ^[+-]?[0-9]*(\.[0-9]{0,5})?$.
In my case I made a minor modification, you seem to be matching either a decimal number or else a whole number. In my case, I modified the regular expression to take a whole number with an optional decimal section.
As is, the regular expression will match values like: .222, 23.22222 but not 4d.22222, 33.333333, etc.
var n1 = "4.33254";
var n2 = "4d.55";
eval("var stringvar=/^[+-]?[0-9]*(\\.[0-9]{0,5})?$/");
alert(stringvar.test(n1));
alert(stringvar.test(n2));
Yielded: true and false respectively.

Convert String to Integer in JavaScript

I have a string value of 41,123 and I want to convert it to an integer in JavaScript.
I tried parseInt(41,123, 10) and parseFloat, but nothing gives me correct answer.
ParseInt, parseFloat work well until comma is encountered, but to above the result is '41'.
Does anyone have an idea about the fix?
var myInt = parseInt("41,123,10".replace(/,/g,""));
Here is the solution:
Number(s.replace(/,/g, ""))
Regex is needed in replacement to remove all comma characters, otherwise only one comma will be replaced.
You can remove the comma, and then parse; let's say the number is variable s:
parseInt(s.replace(/,/g, '')

Categories

Resources