How to convert Hexadecimal, octal,binary
tháng 4 13, 2016 / in javascript, javascript Beginner / with No comments /
Syntax
variable.toString() //convert to String
variable.toString(2) //convert to Binary
variable.toString(8) //convert to Octal
variable.toString(16) //convert to HexadecimalExample
var num=66;
num.toString(); //return "66"
num.toString(2); //return "1000010"
num.toString(8); //return "102"
num.toString(16); //return "42" Example Program:- (Editor)
Editor is Loading...
Output:-
Advertisement
parseInt() in javascript
tháng 4 11, 2016 / in javascript, javascript Beginner / with No comments /
Syntax
parseInt(string, radix);string
The value to parse. If string is not a string, then it is converted to a string (using the ToString abstract operation). Leading whitespace in the string is ignored.
radix
An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified, usually defaulting the value to 10.
Description
The parseInt function converts its first argument to a string, parses it, and returns an integer or NaN. If not NaN, the returned value will be the decimal integer representation of the first argument taken as a number in the specified radix (base). For example, a radix of 10 indicates to convert from a decimal number, 8 octal, 16 hexadecimal, and so on. For radices above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), A through F are used.
If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values. Leading and trailing spaces are allowed.
- If radix is undefined or 0 (or absent), JavaScript assumes the following:
- If the input string begins with "0x" or "0X", radix is 16 (hexadecimal) and the remainder of the string is parsed.
- If the input string begins with "0", radix is eight (octal) or 10 (decimal). Exactly which radix is chosen is implementation-dependent. ECMAScript 5 specifies that 10 (decimal) is used, but not all browsers support this yet. For this reason always specify a radix when using parseInt.
- If the input string begins with any other value, the radix is 10 (decimal).
- If the first character cannot be converted to a number, parseInt returns NaN.
For arithmetic purposes, the NaN value is not a number in any radix. You can call the isNaN function to determine if the result of parseInt is NaN. If NaN is passed on to arithmetic operations, the operation results will also be NaN.
To convert number to its string literal in a particular radix use intValue.toString(radix).
The following example return 15.
Example
parseInt(" 0xF", 16);
parseInt(" F", 16);
parseInt("17", 8);
parseInt(021, 8);
parseInt("015", 10);
parseInt(15.99, 10);
parseInt("15,123", 10);
parseInt("FXX123", 16);
parseInt("1111", 2);
parseInt("15*3", 10);
parseInt("15e2", 10);
parseInt("15px", 10);
parseInt("12", 13);The following example return NaN.
Example
parseInt("Hello", 8); // Not a number at all
parseInt("546", 2); // Digits are not valid for binary representationsThe following example return -15.
Example
parseInt("-F", 16);
parseInt("-0F", 16);
parseInt("-0XF", 16);
parseInt(-15.1, 10);
parseInt(" -17", 8);
parseInt(" -15", 10);
parseInt("-1111", 2);
parseInt("-15e1", 10);
parseInt("-12", 13);The following example return 224.
Example
parseInt("0e0", 16); Example Program:- (Editor)
Editor is Loading...
Output:-
Advertisement
parseFloat() in javascript
tháng 4 11, 2016 / in javascript, javascript Beginner / with No comments /
parseFloat(string);
parseFloat("3.14");
parseFloat("314e-2");
parseFloat("0.0314E+2");
parseFloat("3.14more non-digit characters"); Example Program:- (Editor)
Editor is Loading...
Output:-
Advertisement
toFixed() in javascript
tháng 4 04, 2016 / in javascript, javascript Beginner / with No comments /
digits
Optional. The number of digits to appear after the decimal point; this may be a value between 0 and 20, inclusive, and implementations may optionally support a larger range of values. If this argument is omitted, it is treated as 0.
Returns
A string representation of numObj that does not use exponential notation and has exactly digits digits after the decimal place. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length. If numObj is greater than 1e+21, this method simply calls Number.prototype.toString() and returns a string in exponential notation.
Throws
RangeErrorIf digits is too small or too large. Values between 0 and 20, inclusive, will not cause a RangeError. Implementations are allowed to support larger and smaller values as well.
TypeError
If this method is invoked on an object that is not a Number.
Syntax
number.toFixed([digits]);Example
(6.3652128).toFixed(2); //6.37 Example Program:- (Editor)
Editor is Loading...
Output:-
Advertisement
toPrecision() in javascript
tháng 4 04, 2016 / in javascript, javascript Beginner / with No comments /
A string representing a Number object in fixed-point or exponential notation rounded to precision significant digits. See the discussion of rounding in the description of the Number.prototype.toFixed() method, which also applies to toPrecision().
If the precision argument is omitted, behaves as Number.prototype.toString(). If the precision argument is a non-integer value, it is rounded to the nearest integer.
Syntax
number.toPrecision([precision]);Example
(3.666621).toPrecision(2) //3.37 Example Program:- (Editor)
Editor is Loading...
Output:-
Advertisement
toExponential() in javascript
tháng 3 29, 2016 / in javascript, javascript Beginner / with No comments /
Parameters
fractionDigits
Optional. An integer specifying the number of digits after the decimal point. Defaults to as many digits as necessary to specify the number.
Returns
A string representing a Number object in exponential notation with one digit before the decimal point, rounded to fractionDigits digits after the decimal point. If the fractionDigits argument is omitted, the number of digits after the decimal point defaults to the number of digits necessary to represent the value uniquely.
If you use the toExponential() method for a numeric literal and the numeric literal has no exponent and no decimal point, leave a space before the dot that precedes the method call to prevent the dot from being interpreted as a decimal point.
If a number has more digits than requested by the fractionDigits parameter, the number is rounded to the nearest number represented by fractionDigits digits. See the discussion of rounding in the description of the toFixed() method, which also applies to toExponential().
Throws
RangeError
If fractionDigits is too small or too large. Values between 0 and 20, inclusive, will not cause a RangeError. Implementations are allowed to support larger and smaller values as well.
TypeError
If this method is invoked on an object that is not a Number.
Syntax
numberobject.toExponential([fraction_digit]);Example
var numObj = 77.1234;
console.log(numObj.toExponential()); // logs 7.71234e+1
console.log(numObj.toExponential(4)); // logs 7.7123e+1
console.log(numObj.toExponential(2)); // logs 7.71e+1
console.log(77.1234.toExponential()); // logs 7.71234e+1
console.log(77 .toExponential()); // logs 7.7e+1 Example Program:- (Editor)
Editor is Loading...
Output:-
Advertisement
Math.min() in javascript
tháng 3 29, 2016 / in javascript, javascript Beginner / with No comments /
If no arguments are given, the result is Infinity.
If at least one of arguments cannot be converted to a number, the result is NaN.
Syntax
Math.min([value1,value2,...]);Example
Math.min(20,50,36,2); //2
Math.min(-3,3,0,99); //-3 Example Program:- (Editor)
Editor is Loading...
Output:-
Advertisement
Math.max() in javascript
tháng 3 29, 2016 / in javascript, javascript Beginner / with No comments /
If no arguments are given, the result is -Infinity.
If at least one of arguments cannot be converted to a number, the result is NaN.
Syntax
Math.max([value1,value2,value3,...]);Example
Math.max(50, 10); // 50
Math.max(-1, -200); // -1
Math.max(-999, 2); // 2 Example Program:- (Editor)
Editor is Loading...
Output:-
Advertisement
Math.floor() in javascript
tháng 3 29, 2016 / in javascript, javascript Beginner / with No comments /
Syntax
Math.floor(value); Example
Math.floor( 40.95); // 40
Math.floor(-48.95); // -49 Example Program:- (Editor)
Editor is Loading...
Output:-
Advertisement
Math.random() in javascript
tháng 3 29, 2016 / in javascript, javascript Beginner / with No comments /
Math.random();
Math.random(); //0 to 1 Numbers
(Math.floor(Math.random() * 100) + 1) // 1 to 100 Numbers Example Program:- (Editor)
Editor is Loading...
Output:-
Advertisement
Math.round() in javascript
tháng 3 29, 2016 / in javascript, javascript Beginner / with No comments /
Because round() is a static method of Math, you always use it as Math.round(), rather than as a method of a Math object you created (Math has no constructor).
Syntax
Math.round(number);Example
Math.round(19.39); //19
Math.round(19.7); //20
Math.round(-19.04); //-19
Math.round(-19.51); //-20 Example Program:- (Editor)
Editor is Loading...
Output:-
Advertisement
Math.ceil() in javascript
tháng 3 29, 2016 / in javascript, javascript Beginner / with No comments /
Syntax
Math.ceil(number);Example
Math.ceil(.97); // 1
Math.ceil(0.3); // 1
Math.ceil(3); // 3
Math.ceil(7.003); // 8 Example Program:- (Editor)
Editor is Loading...
Output:-
Advertisement
abs() pow() and sqrt() in javascript
tháng 3 27, 2016 / in javascript, javascript Beginner / with No comments /
Math.pow() - The Math.pow() function returns the base to the exponent power, that is, baseexponent.
Math.sqrt() - The Math.sqrt() function returns the square root of a number.
Example Program:- (Editor)
Editor is Loading...
Output:-
Advertisement
Simple example for array in javascript
tháng 3 26, 2016 / in javascript, javascript Beginner / with No comments /















