Home

Number Manipulation

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

Top

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"

Numbers to a formatted string

.toLocaleString()
Will return you a formatted string from a value (eg... 123456789.99 to 123,456,789.99)

Assume n = 123456789.99
var z = n.toLocaleString() will = "123,456,789.99"
(It's probably best to use the "en-GB" locale setting rather than leave it out)
var z = n.toLocaleString("en-GB") will = "123,456,789.99"
var z = n.toLocaleString("en-GB", {style: 'currency', currency: 'GBP'}) will = "£123,456,789.99"
var z = n.toLocaleString("en-US", {style: 'currency', currency: 'USD'}) will = "$123,456,789.99"

Assume n = 123456789
var z = n.toLocaleString("en-GB", {style: 'currency', currency: 'GBP'}) will = "£123,456,789.00"
Assume n = 123456789.567
var z = n.toLocaleString("en-GB", {style: 'currency', currency: 'GBP'}) will = "£123,456,789.57"
var z = n.toLocaleString("en-GB", {maximumFractionDigits: 2}) will = "123,456,789.57"

NOTE...if n = "123456789" (a string) rather than 123456789 (an actual number) then toLocaleString(...) will NOT work.
You will get back "123456789" : You could use ...

var z = Number(n).toLocaleString("en-GB", {style: 'currency', currency: 'GBP'}) will = "£123,456,789.00"

I liked best...
I created a function called formatNumber which looks like:

const formatNumber = number => {
   // If number is NOT a number then return number asis
   if(isNaN(number)) {
      return number;
   }
   // If I get here then I know it's a number (or a number string)
   return Number(number).toLocaleString("en-GB");
}


A function for formatting currency could be...
const formatNumber = number => {
   // If number is NOT a number then return number asis
   if(isNaN(number)) {
      return number;
   }
   // If I get here then I know it's a number (or a number string)
   return Number(number).toLocaleString("en-GB", {style: 'currency', currency: 'GBP'});
}


Top