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 last year.
Improve this question
const a = 043
console.log(Number(a))
Here the variable a is octal because of which we get the result as 35.
Instead, I want the variable a to be a number 43.
Found results for removing leading zeros from a string(Remove leading zeros from a number in Javascript)
Because your value starts with 0, and its type is number instead of string, it will be recognized as octal, and when you use it directly, javascript will convert it to decimal
The fastest way you can use Number.prototype.toString(radix)
let a = 043
console.log(a.toString(8))
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 6 months ago.
Improve this question
I have a string that looks like "{`index`:`20`,`value`:`RA`}<1", and I want it to become "`RA`<1". I don't think the replace function is sufficient as the index and value changes based on what I enter. Is there a way to do that?
assuming the required string is everything after the last :, except for the }, the original string can be sliced and edited as follows:
const string = "{`index`:`20`,`value`:`RA`}<1";
let newString = string.slice(string.lastIndexOf(":")+1).split("}").join("");
console.log(newString);
This will always append everything after the final } to everthing before it but after the last :, regardless of the earlier content.
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 1 year ago.
Improve this question
I'm trying to check if a number has a 2 decimal place precision. I know how to convert a number to a 2 decimal place precision like this:
var myNumber = 2.456;
var currentNumber = parseFloat(myNumber);
currentNumber = currentNumber.toFixed(2);
console.log(currentNumber);
so If I enter:
1 this will turn into 1.00
2.457 this will turn into 2.46
but how can I check if the result of my code has a 2 decimal place precision? Thanks a lot in advance!
Parse the number into a string, split by a period and check whether the second item's length is 2:
function isPrecise(num){
return String(num).split(".")[1]?.length == 2;
}
console.log(isPrecise(1.23))
console.log(isPrecise(1.2))
console.log(isPrecise(1))
console.log(isPrecise("1.23")) //works if its a string too
You can also use a regex:
function isPrecise(num){
return /\d+\.\d{2}/gm.test(String(num));
}
console.log(isPrecise(1.23))
console.log(isPrecise(1.2))
console.log(isPrecise("1.23")) //works if its a string too
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 1 year ago.
Improve this question
I have the following string:
12.0024.0024.0024.0036.0012.0012.0012.004.006.008.004.004.0012.0012.0012.00
I want to create an array that is split after 2 decimal place, that should look something like this, if I were to instantiate it:
var numArray = new Array ('12.00', '24.00', '24.00', '24.00', '36.00', '12.00', '12.00', '12.00', '4.00', '6.00', '8.00', '4.00', '4.00', '12.00', '12.00', '12.00')
You might be able to split the input string at the interface between a non zero digit on the left and a zero digit on the right:
var input = "12.0024.0024.0024.0036.0012.0012.0012.004.006.008.004.004.0012.0012.0012.00";
var nums = input.split(/(?<=0)(?=[^\D0])/);
console.log(nums);
But, this only coincidentally works here because every float number in the string happens to end with trailing zeroes. If that were not the case, the above method would fail. A better approach would be to manage your data better and not work with such cryptic number strings.
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 7 years ago.
Improve this question
I have a string like command--o1--o2--o3 ( command,o1,o2,o3 are arbitrary words)
And I want get [o1, o2, o3] though a Regular Expression(Not a array operation or other ways. JUST only use Regular Expression).
Is there any idea to accomplish this !?
If you're using JavaScript, and assuming you want all strings after a --, you may do
var things = str.split(/--/).slice(1)
If you just want to get the 2 characters words following --, then you may use
var things = str.match(/--\w\w/g).map(function(s){ return s.slice(2) })
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 8 years ago.
Improve this question
I have a string like
count-contribute-1
count-contribute-11
count-contribute-1111
Here I want to split the string and get the last split value (i.e 1 , 11, 1111);
How can I do it?
split() on - and pop() of the last value
string.split('-').pop()
Use .pop() to get the last item from the array created by .split()
"count-contribute-1".split('-').pop();
Also you can get last part of numbers using regular expression. Like this:
s = "count-contribute-111"
s.match(/\d+$/)
//return "111"
It doesn't matter what separator you use.
s = "count-contribute-+*%111"
s.match(/\d+$/)
//return "111"