There are various methods that can be used to manipulate the numbers in Javascript.
Or are useful to know...
See Math. object for more
or
See Number. reference for more
parseInt(myString)
Returns the value as an integer.
parseInt("123") will give an answer of 123
parseInt("123ABCD") will give an answer of 123
parseInt("123 456") will give an answer of 123
parseInt("ABCD123") will give an answer of NaN (Not a Number)
parseInt("ABCD123").toString() will give an answer of "NaN"
parseInt("123.456") will give an answer of 123
var y = parseInt("ABC123");
if (isNaN(y)) {alert("Failed - Not a Number")};
may be a good way to check if it's a number if you want to know if
parseInt() worked. This way 'y' would = NaN and not 123.
So it depends if you want "123ABC" to be an invalid number or not.
parseFloat(myString)
Returns the value as a floating point.
parseFloat("123") will give an answer of 123
parseFloat("123ABCD") will give an answer of 123
parseFloat("123 456") will give an answer of 123
parseFloat("ABCD123") will give an answer of NaN (Not a Number)
parseFloat("ABCD123").toString() will give an answer of "NaN"
parseFloat("123.456") will give an answer of 123.456
var y = parseFloat("ABC123");
if isNaN(y) == "NaN") {alert("Failed - Not a Number")};
is another way to check if it's a number, if you want to know if
parseFloat() worked.
isNaN(myString)
Checks to see if the value is Not a Number
isNaN("123") will give an answer of FALSE as it is a number
isNaN("123ABCD") will give an answer of TRUE as it is Not a Number
isNaN("123 456") will give an answer of TRUE
isNaN("ABCD123") will give an answer of TRUE
isNaN("5-2") will give an answer of FALSE (it reckons 5-2 is a number)
isNaN(true) will give an answer of FALSE (true is 1 / false is 0)
isNaN(null) will give an answer of FALSE (it reckons null is a number)
typeof(myString)
Checks to see what the variable type is
typeof("123") will give an answer of "string"
typeof("123ABCD") will give an answer of "string"
typeof(123) will give an answer of "number"
typeof(true) will give an answer of "boolean"
typeof(myFunction) will give an answer of "function"
typeof(myObject) will give an answer of "object"
typeof 123 will give an answer of "number"
typeof "123" will give an answer of "string"
typeof true will give an answer of "boolean"
eg
if (typeof myString === "string") {...}
or... if (typeof(myString) === "string") {...}
Math.round(myString)
Rounds to the nearest integer.
Math.round("123") will give an answer of 123
Math.round("123ABCD") will give an answer of NaN as it is Not a Number
Math.round("123.456") will give an answer of 123
Math.round(123.567) will give an answer of 124
Math.round("-123.456") will give an answer of -123
Math.round(-123.567) will give an answer of -124
Math.floor(myString)
Rounds DOWN to the nearest integer.
Math.floor("123") will give an answer of 123
Math.floor("123ABCD") will give an answer of NaN as it is Not a Number
Math.floor("123.456") will give an answer of 123
Math.floor(123.567) will give an answer of 123
Math.floor("-123.456") will give an answer of -124 (-124 is less than -123)
Math.floor(-123.567) will give an answer of -124
Math.ceil(myString)
Rounds UP to the nearest integer.
Math.ceil("123") will give an answer of 123
Math.ceil("123ABCD") will give an answer of NaN as it is Not a Number
Math.ceil("123.456") will give an answer of 124
Math.ceil(123.567) will give an answer of 124
Math.ceil("-123.456") will give an answer of -123
Math.ceil(-123.567) will give an answer of -123 (-123 is greater than -124)
Math.trunc(myString)
Returns the integer part of the number.
Math.trunc("123") will give an answer of 123
Math.trunc("123ABCD") will give an answer of NaN as it is Not a Number
Math.trunc("123.456") will give an answer of 123
Math.trunc("123.567") will give an answer of 123
Math.trunc("-123.456") will give an answer of -123
Math.trunc(-123.567) will give an answer of -123
Math.random()
Returns a random number between 0 and 0.99999999.
Math.random() will give an answer between 0 and 0.99999999
Math.floor((Math.random()*10)+1) will give an answer between 1 and 10
Math.floor((Math.random()*100)+1) will give an answer between 1 and 100
Math.floor((Math.random()*10)+8) will give an answer between 8 and 17
Math.floor((Math.random()*5)+1) will give an answer between 1 and 5
Math.floor((Math.random()*5)+1)+6 will give an answer between 7 and 11
Math.abs(myString)
Returns the absolute value of a number.
Math.abs("123") will give an answer of 123
Math.abs(10.5) will give an answer of 10.5
Math.abs(-10.5) will give an answer of 10.5
Number(myString)
Returns different object values to their numbers.
Number("123") will give an answer of 123
Number("123ABCD") will give an answer of NaN as it is Not a Number
Number("123.456") will give an answer of 123.456
Number("123.567") will give an answer of 123.567
Number("-123.456") will give an answer of -123.456
Number(-123.567) will give an answer of -123.567
Number(true) will give an answer of 1
Number(false) will give an answer of 0
Number(Date()) will give an answer like 160926633135 (No. of milliseconds)
Number(null) will give an answer of 0
myNum.toFixed(n)
Converts a number (not a string/object) into a string, rounding the number to 'n' decimal places.
myNum = 123 ... myNum.toFixed(2) will give an answer of "123.00"
myNum = 123.456 ... myNum.toFixed(2) will give an answer of "123.46"
myNum = 123.567 ... myNum.toFixed(2) will give an answer of "123.57"
myNum = -123.456 ... myNum.toFixed(2) will give an answer of "-123.46"
myNum = -123.567 ... myNum.toFixed(2) will give an answer of "-123.57"
myNum = -123.456 ... myNum.toFixed(0) will give an answer of "-123"
myNum = -123.567 ... myNum.toFixed(0) will give an answer of "-124"