As the title says, which function will give me a result similar to what .substr() does, only for integers?
Thanks!
UPDATE:
Here is what isn't working:
if ($(#itemname).val() == "Not Listed") {
var randVal = Math.random() * 10238946;
var newVal = randVal.toString().substr(0, 4);
$("#js_itemid").val(randVal);
$("#js_price").val("199.99");
}
substring(int beginIndex, int endIndex) method returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.
The substring() method returns the part of the string between the start and end indexes, or to the end of the string.
Trunc() method. The Math. trunc() is the most popular method to remove the decimal part of JavaScript. It takes positive, negative, or float numbers as an input and returns the integer part after removing the decimal part.
The substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters. The substring() method returns the part of the string between the start and end indexes, or to the end of the string.
What about ...
var integer = 1234567;
var subStr = integer.toString().substr(0, 1);
... ?
var a = 234;
There are several methods to convert a number to a string in order to retrieve the substring:
Included are examples of how the given number, a
, may be converted/coerced.
(a+'').substr(1,1); // "3"
a.toString().substr(1,1) // "3"
`${a}`.substr(1,1) // "3"
String(a).substr(1,1) // "3"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With