This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to parseInt a string with leading 0
If I parseInt("01") in javascript its not the same as parseInt("1")???
start = getStartEugene("MN01");
start2 = getStartEugene("MN1");
getStartEugene: function(spot) //ex: GT01 GT1
{
var yard = spot.match(/[0-9]+/);
var yardCheck = parseInt(yard);
if (yardCheck < 10)
return "this"+yard;
else
return "this0"+yard
}
I want something to be returned as this+2 digits such as this25, this55, this01, this02, this09
But i am not getting it. Anyone know why?
You need to add the radix (2nd) argument to specify you are using a base 10 number system...
parseInt("01", 10); // 1
This happens because Javascript interprets numbers starting with zero as an octal (base 8) number. You can override this default behaviour by providing the base in which the string will be evaluated (as #jondavidjohn correctly pointed).
parseInt("10"); // returns 10
parseInt("010"); // returns 8
Related
This question already has answers here:
How can I pad a value with leading zeros?
(76 answers)
Closed 9 years ago.
When you convert a small number to a hexadecimal representation, you need leading zeroes, because toString(16) will return f for 15, instead of 00000f. Usually I will use the loop like this:
var s = X.toString(16); while (s.length < 6) s = '0' + s
is there a better way in JavaScript?
UPD: The answer suggested answer How can I create a Zerofilled value using JavaScript? is not what I am looking for, I look for a very short code, that is suited specifically for 24 bit integers converted to a hexadecimal looking strings.
How about
('00000'+(15).toString(16)).substr(-5)
Maybe this:
var s = X.toString(16);
s = "000000".substr(0, 6 - s.length) + s;
This question already has answers here:
Coerce to number
(4 answers)
Closed 9 years ago.
Going though the asm.js documentation I've observed this strange (to me at least, quite new to JS) snippet all over the sample code:
function test(x) {
x = +x; // THIS
...
return +(x*y);
}
What is the purpose of the + on the first line?
Its simply used for casting a value with another type to number. Additonally it will return NaN if the value after that + symbol could not get converted into a number.
FIDDLE
From the book Javascript and Jquery - The Missing Maunal
var numOfShoes = '2';
var numOfSocks = 4;
var totalItems = +numOfShoes + numOfSocks;
Adding a + sign before a variable (make sure there’s no space between the two) tells
the JavaScript interpreter to try to convert the string to a number value—if the string
only contains numbers like “2”, you’ll end up with the string converted to a number.
In this example, you end up with 6 (2 + 4). Another technique is to use the Number()
command like this:
var numOfShoes = '2';
var numOfSocks = 4;
var totalItems = Number(numOfShoes) + numOfSocks;
Number() converts a string to a number if possible. (If the string is just letters and not
numbers, you get the NaN value to indicate that you can’t turn letters into a number.)
Perhaps I am reading this wrong but from the specs http://asmjs.org/spec/latest/#parameter-type-annotations
is that casting it as a double?
This question already has answers here:
javascript calculation formula is not working
(2 answers)
Closed 10 years ago.
So here is a dorky experiment I put together basically trying to generate a D&D style attack roll with a modifier. I want to add the numbers, but javascript keeps adding the numbers as strings. I'm not sure how to get the basic math done..... Here is my code-
function battle()
{
var CS = document.battleForm.playerCS.value;
var D20 = Math.ceil(Math.round(Math.random() * 20))
var attackRoll = CS + D20
if (isNaN(CS))
{
alert ("please provide your Combat Score!")
return
}
if (CS != '')
{
document.battleForm.enemyCS.value = attackRoll
}
}
To ensure two numbers are added together, try:
var num3 = +num1 + (+num2);
This could be preferred over the use of parseInt or parseFloat for two reasons:
+ will convert any number (meaning, you don't need a different method for an integer and a float)
+ will fail if either value are not convertible. parseInt and parseFloat ignore any trailing text in the variable. So for example, parseInt("10px", 10) results in 10, while (+"10px") results in NaN.
It's up to you what you want to use.
var CS = parseInt(document.battleForm.playerCS.value, 10); // or parseFloat if you expect float number
The value of any form text is a string, so you need to convert it to number.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to match a number which is less than or equal to 100?
i need a regular expression between these two values 1000 <= x <= 500000 im trying with this one that i constructed but doesnt seem to work
/(1[8-9]|[8-9]|[8-9]|5[0-9]|[0-9]|[0-9]|[0-9]|[0-9])/
Any ideas? thanks in advance!
Is there a particular reason you don't just test the numbers as numbers?
var yourNum = parseInt(yourString, 10); // use parseFloat if it has decimals
if (yourNum >= 1000 && yourNum <= 500000) {
// success
} else
// fail
}
\b([1-9][0-9]{3,4}|[1-4][0-9]{5}|500000)\b
Match the cases 1000-9999, 10000-99999, 100000-499999 or 500000:
([1-9]\d{3}|[1-9]\d{4}|[1-4]\d{5}|500000)
Or combining the two first:
([1-9]\d{3,4}|[1-4]\d{5}|500000)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Workarounds for JavaScript parseInt octal bug
I am trying to parse an integer number.
a = parseInt("0005") <- gives 5
a = parseInt("0008") <- gives 0
Can someone explain what's happening? It doesn't make any sense to me.
When parseInt has a leading 0 and a radix parameter isn't specified, it assumes you want to convert the number to octal. Instead you should always specify a radix parameter like so:
a = parseInt("0008", 10) // => 8
Numbers starting with 0 are parsed as octal by parseInt, unless you specify a radix to use.
You can force parseInt to parse as decimal by doing
a = parseInt("0008", 10)