The Number() Constructor
Number constructor. Just pass the string as the first argument in
var string = '123'; var num = Number(string);
Number removes decimal places and will give a
NaN error if any other separators are included in the string to be converted.
Number('20,000') //NaN Number('20.00') //20
parseInt works just like
Number, except we can pass a second argument to convert the string into non-decimal numerical systems such as binary.
parseInt will also treat non
. (dot) separators as a decimal place also.
parseInt('222.222'); // decimal place will be removed
parseInt('222kk222'); // everything after first non number is cut
parseInt("0xF", 16); // convert hexadecimal
parseInt('s123'); // strings starting with non-numerical value will return NaN
parseFloat will preserve floating-point numbers as they are converted from strings.
parseFloat('22.00'); // decimal place is removed from floating points consisting of only zeros.
Just like with
parseInt, everything after a non-numerical value is removed and strings starting with a non-numerical value will be returned as
parseFloat('222kk222'); // everything after first non number is cut
If there are any non-numerical characters in the string it will output
Math.ceil will also accept strings and convert them into number objects, making it a clean way to simultaneously convert a numerical string and round it up. It has the same behaviour as
Math.floor in terms of what numerical strings it will accept.
Multiply by 1
var string = '22.72'; string * 1