This question already has answers here:
How to convert an integer to an array in PHP?
(4 answers)
Closed 8 years ago.
I have 23681 its not string but integer , i want to make an array by placing each number on an index in php or javascript any guide would be appreciated
[0]=>2
[1]=>3
[2]=>6
[3]=>8
[4]=>1
Javascript
result = (23681).toString().split("").map(Number);
You can use str_split():
$array = str_split($yourNumber);
Your int will be casted to string automatically. So no implicit casting needed here.
Another javaScript solution without string conversion
var number = 12345, result=[];
while(number>0) {
result.push(number % 10);
number = Math.floor(a / 10);
}
result.reverse()
quick and easy:
$your_number_arr = array_map('intval', str_split($your_number));
edit: integer conversion
You should be more specific. I don't know if you need this in Javascript or PHP, but here it is in php:
$str = ((string)$int);
$array = str_split($str);
Related
This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 2 years ago.
I get one string by query like '5e6,5e4,123'.
And I want to make an array containing this query as below in JS.
['5e6', '5e4', '123']
How can I make this? Thank you so much for reading it.
You can use .split(',')
var str = "5e6,5e4,123";
var array = str.split(',');
console.log(array);
You can read more on this here
Use String.split:
console.log('5e6,5e4,123'.split(","))
var query = '5e6,5e4,123';
var queries = query.split(‘,’);
You can make use of split method of string like below:
var res = str.split(',');
const output = input.split(',');
This question already has answers here:
Getting parts of a URL with JavaScript
(5 answers)
Closed 5 years ago.
url:
http://xxxxxx.com/video/view/12345
Can I take 12345 in the url using javascript?
Please help me
Use RegExp, Array#match and negative lookahead.
var str = 'http://xxxxxx.com/video/view/12345';
console.log(str.match(/(?!view\/)\d+/)[0]);
You can also try this if you're sure that it'll always be in last:
var num = location.pathname.split('/').pop(); // "12345"
and further: parseInt(num);
You can parse your URL with the following code. Then just get the last part.
var url = 'http://xxxxxx.com/video/view/12345';
var url_parts = url.replace(/\/\s*$/,'').split('/');
console.log(url_parts[url_parts.length - 1]); // last part
This question already has answers here:
How to convert a string to an integer in JavaScript
(32 answers)
Closed 7 years ago.
I am trying to calculate Body Mass Index by receiving Weight and Height from HTML form and passing it to JavaScript code by .getElementById();. But, as far as I understand, type of data received from form is "string" and I need to receive a "number". How can I solve this problem?
Use parseInt().
var a = "10";
var b = parseInt(a);
See : http://www.w3schools.com/jsref/jsref_parseint.asp
var a = document.getElementById("whatever").value;
// You can use any 3
+a; // Unary Operator
parseInt(a, 10); // parseInt with radix 10
Number(a);
you can use function parseInt(string) that Convert string into int .Now in your senario :-
var weight_str = document.getElementByID("weight");
var weight_int = parseInt(weight_str);
this will give you the result.
This question already has answers here:
Why won't my inputs value sum?
(5 answers)
Closed 8 years ago.
i am trying to make a maths engine with JavaScript and HTML.
Here is (http://i.stack.imgur.com/NCEa4.jpg)
The alert from the js alert() function works but the output from it is wrong:
'HTMLObject'
Value of the input field is always a string. So when you use + operator with two strings it concatenates them. If you want to add two numbers you need first to convert strings to numbers. There are multiple ways to do it, for example:
var plus = parseInt(one) + parseInt(two);
Or you can use Number(one), or another unary + operator: +one + +two, but this might look confusing.
Use 'parseInt()' function to convert those values to integers first and then add the values.
make your variable plus like this:
var plus = parseInt(one) + parseInt(two);
You need to enclose your function code in curly braces { & }.
So Use:
function Maths(){
var one=document.getElementById("fid").value;
var two=document.getElementById("sid").value;
var plus=Math.parseInt(one)+Math.parseInt(two);
alert(plus);
}
Also use parseInt() to make data type conversion to convert to int in JavaScript.
Hope it'll help you. Cheers :)!!
Will be better if you use second argument in parseInt for be sure that value will be in decimal system.
function Maths() {
var one = document.getElementById("fid").value,
two = document.getElementById("sid").value,
plus = Math.parseInt(one, 10) + Math.parseInt(two, 10);
alert(plus);
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I Convert a String into an Integer in JavaScript?
I have a select element that contains options for products. What I want to achieve is when they pick an option the price on the page automatically adjusts. I have figured out how to retrieve those values but when I combine them it just puts two numbers together instead of actually adding them.
For example instead of outputting 60 when I have 50 + 10 it outputs 5010.
My code:
$('.product_options').change(function(){
var base_price = $('#base_price').html();
var add_price = $(this).find("option:selected").data('price');
var new_price = base_price+add_price;
console.log(new_price);
$('.current_price').html(base_price+add_price);
});
Is there a way I can convert them both to integers so the operation actually goes through?
Thanks in advance!
Use parseInt
$('.product_options').change(function(){
var base_price = parseInt($('#base_price').html(), 10); // 10 as second argument will make sure that base is 10.
var add_price = parseInt($(this).find("option:selected").data('price'), 10);
var new_price = base_price+add_price;
console.log(new_price);
$('.current_price').html(base_price+add_price);
});
Try:
var base_price = +$('#base_price').html();
var add_price = +$(this).find("option:selected").data('price');
See the mighty: Mozilla's Arithmetic Operators Reference - Unary Negation
Any values you pull out of the DOM are going to be strings, and need converting into number types before you can do mathematical operations with them.
parseInt( ... ) is a built in javascript function that converts a string into an integer, if the string consists of digits only.
If you need a decimal number, you can use parseFlaot.
var new_price = parseInt(base_price)+parseInt(add_price);
// new_price is now set to the sum of `base_price` and `add_price`
Use parseFloat or parseInt
$('.product_options').change(function(){
var base_price = $('#base_price').html();
var add_price = $(this).find("option:selected").data('price');
var new_price = parseFloat(base_price) + parseFloat(add_price);
console.log(new_price);
$('.current_price').html(base_price+add_price);
});
Yes there is.
intval = parseInt(string)
is what you're looking for.