Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to convert a 17digit number string into a number
this is the number "76561197962169398".I tried using parseInt()
The result of using parseInt is :-
76561197962169390
I am loosing the last digit.I also tried BigInt() 'n' is getting appended to the number.
I m thinking of using replace() with a regex for only digits.
Is there any other way I can achieve this without loosing precision.
Please any help regarding this is really appriciated.THANK YOU
in chrome 83 devtools:
x=76561197962169398n
76561197962169398n
++x
76561197962169399n
typeof x
"bigint"
y=BigInt("76561197962169398")
76561197962169398n
++y
76561197962169399n
x+y
153122395924338798n
x + 1
VM342:1 Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions
at <anonymous>:1:2
(anonymous) # VM342:1
x + 1n
76561197962169400n
[5n, 3n, 9n, 7n].sort()
[3n, 5n, 7n, 9n]
The n suffix is for display - and in code it's needed to say a literal value needs to be treated as bigint instead of number - think of it like quotes for strings - without quotes a sequence of characters is not a string - similarly a number without n suffix is not a bigint - it's a number that has limited precision and simply cannot be used for large values
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
What is the best possible way to create a function that transforms a string of upvote counts into an array of numbers. Each k represents a thousand.
transformUpvotes("6.8k 13.5k") ➞ [6800, 13500]
transformUpvotes("5.5k 8.9k 32") ➞ [5500, 8900, 32]
transformUpvotes("20.3k 3.8k 7.7k 992") ➞ [20300, 3800, 7700, 992]
Return the upvotes as an array.
Now i tried to do this myself with or without regex, the pattern i used was this /\.\d(k)/g
I first converted the string into a javascript array using array.split(' '); but i don't know how to replace the k with zeroes properly so that the k after floating point get two zeroes and a k without floating point get three zeroes.
function transformUpvotes(upvotes) {
return upvotes.split(" ").map(x => {
parsed = parseFloat(x);
return x.endsWith("k") ? parsed * 1000 : parsed;
});
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
How to get all number from beginning of string until first non-number?
For example, I want to get 12345 from '12345abc' and another example get 5678 from '5678kkk'.
Is any way can do this?
You could use RegExp#match with ^ anchor, to find out the numeric characters from beginning:
const string = "12345abc";
const matches = string.match(/^\d+/);
// Fallback if no matches found
const numbers = (matches || [])[0];
console.log(numbers);
Use parseInt() as it will stripe out all the characters other than the numeric character so you do not need custom logic for getting the numeric value as you have described:
console.log(parseInt('12345abc'));
The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems).
Use parseInt
let str = '5678kkk';
console.log(parseInt(str));
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
As you can see from the title I have various cases for strings that can contain numbers in them. I found out that using parseInt() and parseFloat() didn't work for me as parseInt will convert number like 10.28 to just 10, but parseFloat will make number like 10 into 10.0, I want to somehow convert string into number so it stays exactly like it was in the string without anything removed or added.
Per MDN Number ( MSDN page also, but not so much info ).
At the top of the page:
The primary uses for the Number object are:
If the argument cannot be converted into a number, it returns NaN.
In a non-constructor context (i.e., without the new operator), Number can be used to perform a type conversion.
At the bottom of the page, there are some examples:
Convert numeric strings to numbers
Number("123") // 123
Number("") // 0
Number("0x11") // 17
Number("0b11") // 3
Number("0o11") // 9
Number("foo") // NaN
Number("100a") // NaN
Demo https://jsfiddle.net/hxkfafdw/
More on the topic - Number("foo") is NaN Number("f00") - same. Number("0xf000") - this is a hex number.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I know that:
alphabet = 'abcdefghijklmnopqrstuvwxyz'
print alphabet[0]
# prints a
print alphabet[25]
#prints z
and so on, but how do I find out the opposite, ie. :
alphabet = 'abcdefghijklmnopqrstuvwxyz'
's' = alphabet[?]
""" The question mark represents that I want to know
what index the letter is in the string."""
In python you could use the find method:
>>> alphabet = 'abcdefghijklmnopqrstuvwxyz'
>>> alphabet.find('a')
0
>>> alphabet.find('b')
1
>>> alphabet.find('c')
>>> alphabet.find('z')
25
Edit to add: Like Warren pointed out you can also use index, the difference being that find will return -1 for the position if not found, and index raises a ValueError when not found.
In javascript, use indexOf:
> "abc".indexOf("b")
1
In javascript, you would use alphabet.indexOf('a').
In Python, to get the position of a certain character inside a string, it would be:
alphabet.index('s')
If you just want to get alphabet indices, this works (Python):
import string
for c in string.ascii_lowercase:
print(ord(c)-97)
This will print the numbers 0-25. The idea here is to make use of unicode points of the char using ord. lowercase chars starts at 97. Another example:
>>> ord('k')-97
10
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
var a = $('.box').width();
var b = $(document).height();
Now, let's say 124px, is an int or string ?
So far I have done this through trial and error, but now I'm curious to understand why if (b < 20), however parseInt is needed to calculate var c = a + b. why is that?
Note: I'm assuming that a and b are strings like "10" and "20", for the purposes of this question. $('.box').width() will actually return 123 (if your element was 123 pixels wide, for example), so in this case parseInt is irrelevant.
Because in
a + b
If you don't use parseInt, then JavaScript will assume you want to concatenate the strings (because + is overloaded for concatenation). However, when you use
b < 20
JS knows that you can't have "a string less than 20," because that makes no sense, so it casts to a number automatically.
We don't.
.width() and height() return numbers, not strings, and so don't need to be converted using parseInt(...,10). I don't know who the "we" is you refer to, but it does not include people who use jQuery correctly.
Try it out on this very website - open up a console, and type var d = jQuery("div"); console.log(typeof d.height(), typeof d.width(), d.height() + d.width());, and you'll see [number], [number], and the result of a normal numeric addition.
because when you use the < operators it only makes sense for numbers to be bigger or smaller, however for the "+", in javascript it could be used to concatenate strings as well so:
"30"+"20"="3020"
and 30+20=50
NOTE: #Mike Poxman Kamerman is right, for the most part it will work without parseInt because the width and height jquery functions return floats but it is the correct practice as javascript used to be very strict and in browsers like older IEs it could not work.
When you are comparing a string and a number, one has to be converted to the type of the other for the comparison to be possible. The string will implicitly be converted to a number.
When you are using the + operator with two strings, there is no conversion needed for the operation to be possible. It will just concatenate the strings, but if you want to use the operator for addition instead of concatenation, then you need to explicitly convert the strings to numbers first.