Header Ads Widget

Ticker

6/recent/ticker-posts

TypeScript tutorial: Lesson 7 – Number

The Number class

Syntax

1
var my_number = new Number(value)

Number Properties:

S.No.Property & Description
1.

MAX_VALUE

The largest possible value a number in TypeScript can have 1.7976931348623157E+308.

2.

MIN_VALUE

The smallest possible value a number in TypeScript can have 5E-324.

3.

NaN

Equal to a value that is not a number.

4.

NEGATIVE_INFINITY

A value that is less than MIN_VALUE.

5.

POSITIVE_INFINITY

A value that is greater than MAX_VALUE.

6.

prototype

A static property of the Number object. Use the prototype property to assign new properties and methods to the Number object in the current document.

7.

constructor

Returns the function that created this object’s instance. By default, this is the Number object.

Number Methods

toExponential(): Forces a number to display in exponential notation as a string , even if the number is in the range in which JavaScript normally uses standard notation.

Syntax

1
number.toExponential( [fractionDigits] )

fractionDigits − An integer specifying the number of digits after the decimal point, must be between 0 and 100. Defaults to as many digits as necessary to specify the number.

Return Value:
A string representing a Number object in exponential notation with one digit before the decimal point, rounded to fractionDigits digits after the decimal point.

Example:

1
2
3
4
5
6
7
var number = 1986.0717
var e1 = number.toExponential()
var e2 = number.toExponential(1)
var e3 = number.toExponential(0)
console.log(e1) //1.9860717e+3
console.log(e2) //2.0e+3
console.log(e3) //2e+3

toFixed(): Formats a number with a specific number of digits to the right of the decimal.

Syntax

1
number.toFixed( [digits] )

digits − The number of digits to appear after the decimal point, must be between 0 and 100. Default value is 0.

Return Value
A string representation of number that does not use exponential notation and has the exact number of digits after the decimal place.

Example:

1
2
3
4
5
var number = 1986.0717
var e1 = number.toFixed()
var e2 = number.toFixed(1)
console.log(e1) //1986
console.log(e2) //1986.1

toLocaleString(): Returns a string value version of the current number in a format that may vary according to a browser’s local settings.

Syntax

1
number.toLocaleString()

Return Value
Returns a human readable string representing the number using the locale of the environment.

Example:

1
2
3
4
var number = 1986.0717
var e1 = number.toLocaleString()
 
console.log(e1) //1,986.072

toPrecision(): Defines how many total digits (including digits to the left and right of the decimal) to display of a number. A negative precision will throw an error.

Syntax

1
number.toPrecision( [ precision ] )

precision − An integer specifying the number of significant digits, must be between 0 and 100.

Return Value
Returns a string representing a Number object in fixed-point or exponential notation rounded to precision significant digits.

Example:

1
2
3
4
5
6
var number = 1986.0717
var e1 = number.toPrecision()
var e2 = number.toPrecision(3)
 
console.log(e1) //1986.07
console.log(e2) //1.99e+3

toString(): Returns the string representation of the number’s value. The function is passed the radix, an integer between 2 and 36 specifying the base to use for representing numeric values.

Syntax

1
number.toString( [radix] )

radix − An integer between 2 and 36 specifying the base to use for representing numeric values.

Return Value:
Returns a string representing the specified Number object, must be between 2 and 36 .

Example:

1
2
3
4
5
6
var number = 1986
var e1 =number.toString()
var e2 =number.toString(16)
 
console.log(e1) //1986
console.log(e2) //7c2

valueOf(): Returns the number’s primitive value.

Syntax
number.valueOf()
Return Value
Returns the primitive value of the specified number object.

Example:

1
2
3
4
var num = new Number("1e2")
var v1 = num.valueOf()
 
console.log(num) //100

Publicar un comentario

0 Comentarios