In TypeScript, the number data type is useful to define variables to accept only numbers. The TypeScript will support all numeric values same as JavaScript and it will store all the numbers as floating-point numbers.
The number
data type in typescript is useful to represent integers as well as floating-point values. The typescript numbers also support decimal, hexadecimal, binary and octal literals.
Following is the syntax of defining the variables with number type in typescript.
[Keyword] [Variable Name]: [number] = [Value];
[Keyword] [Variable Name]: [number];
If you observe the above syntax, we added a number
data type after the variable name to tell the compiler that the number
type of data the variable can hold.
var
, let
or const
to define the scope and usage of variables.Now, we will learn how to use number data types in TypeScript with examples.
Following is the example of defining the variables with number
data type in typescript.
let num1: number = 446; // number
let num2: number = 78.65; // number
let num3: number = 0x120A; // hexadecimal
let num4: number = 0b1110; // binary
let num5: number = 0o211; // octal
console.log(num1); // 446
console.log(num2); // 78.65
console.log(num3); // 4618
console.log(num4); // 14
console.log(num5); // 137
If you observe the above number
type example, we defined multiple variables with number
type to hold the required values including hexadecimal, octal, and binary based on our requirements.
The following are the different types of methods available for number
type in typescript.
Method | Description |
---|---|
toExponential() | It will return the number in exponential notation string format. |
toFixed() | It will return the number in fixed-point notation string format. |
toLocaleString() | It will convert the number to a string by using the current or specified locale. |
toPrecision() | It will return the number in either exponential or fixed-point notation with a specified number of digits in string format. |
toString() | It will return the string representation of number in the specified base. |
valueOf() | It will return the primitive value of a specified object. |
Following is the example of using number
type methods in typescript.
let num1: number = 446;
// toExponential()
console.log('Exponential Method')
console.log(num1.toExponential()); // 4.46e+2
console.log(num1.toExponential(1)); // 4.5e+2
console.log(num1.toExponential(2)); // 4.46e+2
// toString()
console.log('String Method')
console.log(num1.toString()); // 446
console.log(num1.toString(2)); // 110111110
console.log(num1.toString(4)); // 12332
// valueOf()
console.log('ValueOf Method')
console.log(num1.valueOf()); // 446
console.log(typeof num1); // number
let num2: number = 78.65123;
// toFixed()
console.log('Fixed Method')
console.log(num2.toFixed()); // 79
console.log(num2.toFixed(1)); // 78.7
console.log(num2.toFixed(2)); // 78.65
let num3: number = 20456.543;
// toLocaleString()
console.log('LocaleString Method')
console.log(num3.toLocaleString()); // 20,456.543 - US
console.log(num3.toLocaleString('de-DE')); // 20.456,543 - German
let num4: number = 46.6521;
// toPrecision()
console.log('Precision Method')
console.log(num4.toPrecision()); // 46.6521
console.log(num4.toPrecision(1)); // 5e+1
console.log(num4.toPrecision(2)); // 47
console.log(num4.toPrecision(3)); // 46.7
If you observe the above example, we used different methods of number
type based on our requirements.
In typescript, we can also represent the numbers by using Number
object like as shown below.
[Keyword] [Variable Name] = new Number(Value);
Following is the example of using Number
object to represent numbers in typescript.
let num1 = new Number(446);
let num2 = new Number(46.6521);
In case, if we pass the non-numeric argument to Number
object, then it will return the variable value as NaN
.
The typescript Number
object is having different properties to use it in our applications. The following table lists different properties of Number
object in typescript.
Property | Description |
---|---|
MAX_VALUE | It will return the largest number that can be represented in JavaScript. Equal to approximately 1.79E+308. |
MAX_SAFE_INTEGER | It will return the maximum safest integer number that you can use in typescript i.e. 9007199254740991 2^53 − 1 |
MIN_VALUE | It will return the smallest number i.e. 5.00E-324. |
MIN_SAFE_INTEGER | It will return the minimum safest integer number that you can use in typescript i.e. −9007199254740991 (−(2^53 − 1)). |
EPSILON | It will return the value that is difference between 1 and the smallest value greater than 1 which is approximate: 2.220446049250313e-16 |
NEGATIVE_INFINITY | It will return the value that is less than MIN_VALUE. |
POSITIVE_INFINITY | It will return the value that is greater than MAX_VALUE. |
NaN (Not a Number) | The TypeScript will return NaN when the valid number not defined in number calculations. |
parseFloat | It is useful to convert string to a floating-point number. |
parseInt | It is useful to convert string to an integer. |
prototype | It is useful to assign new properties and methods to the Number object in current document. |
Following is the example of using Number
object properties in typescript.
// Number Object Properties
console.log(Number.MAX_VALUE);
console.log(Number.MIN_VALUE);
console.log(Number.POSITIVE_INFINITY);
console.log(Number.NEGATIVE_INFINITY);
// NaN Example
let num1 = 0;
if (num1 <= 0) {
console.log(Number.NaN);
}
else {
console.log(num1);
}
// Prototype Example
function User(id: number, name: string) {
this.id = id
this.name = name
}
let emp = new User(446, "Suresh")
User.prototype.email = "support@tutlane.com"
console.log("Id: " + emp.id)
console.log("Name: " + emp.name)
console.log("Email: " + emp.email)
If you observe the above example, we used different properties of Number
object based on our requirements.
When we execute the above example, we will get the result as shown below.
1.7976931348623157e+308
5e-324
Infinity
-Infinity
NaN
Id: 446
Name: Suresh
Email: support@tutlane.com
This is how we can use number
type in typescript to define the variables to accept numbers based on our requirements.