TypeScript Number

Introduction

In this chapter, we will explore the number type in TypeScript. The number type is used to represent both integer and floating-point values. Understanding how to work with numbers is essential for performing calculations and handling numeric data in your TypeScript programs.

Table of Contents

  • Definition
  • Number Syntax
  • Basic Operations with Numbers
  • Number Methods
  • Examples and Output
  • Conclusion

Definition

In TypeScript, the number type is used to represent numeric values. This includes both integers and floating-point numbers. TypeScript does not differentiate between different numeric types like int, float, or double; all numbers are of type number.

Number Syntax

Syntax

let variableName: number = value;

Example

let age: number = 25;
let pi: number = 3.14;
console.log(age, pi);

Output

25 3.14

Basic Operations with Numbers

TypeScript supports all basic arithmetic operations on numbers, including addition, subtraction, multiplication, division, and modulus.

Example

TypeScript Code

let a: number = 10;
let b: number = 5;

let addition = a + b;
let subtraction = a - b;
let multiplication = a * b;
let division = a / b;
let modulus = a % b;

console.log(`Addition: ${addition}`);       // Output: 15
console.log(`Subtraction: ${subtraction}`); // Output: 5
console.log(`Multiplication: ${multiplication}`); // Output: 50
console.log(`Division: ${division}`);       // Output: 2
console.log(`Modulus: ${modulus}`);         // Output: 0

Output

Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulus: 0

Number Methods

TypeScript provides several built-in methods for working with numbers. These methods are available on the Number object.

Example

TypeScript Code

let num: number = 123.456;

// toFixed
let fixed = num.toFixed(2);
console.log(`Fixed: ${fixed}`); // Output: 123.46

// toExponential
let exponential = num.toExponential(2);
console.log(`Exponential: ${exponential}`); // Output: 1.23e+2

// toPrecision
let precision = num.toPrecision(4);
console.log(`Precision: ${precision}`); // Output: 123.5

Output

Fixed: 123.46
Exponential: 1.23e+2
Precision: 123.5

Examples and Output

Example 1: Basic Number Operations

This example shows how to perform basic arithmetic operations on numbers in TypeScript.

TypeScript Code (src/index.ts)

let a: number = 10;
let b: number = 5;

let addition = a + b;
let subtraction = a - b;
let multiplication = a * b;
let division = a / b;
let modulus = a % b;

console.log(`Addition: ${addition}`);       // Output: 15
console.log(`Subtraction: ${subtraction}`); // Output: 5
console.log(`Multiplication: ${multiplication}`); // Output: 50
console.log(`Division: ${division}`);       // Output: 2
console.log(`Modulus: ${modulus}`);         // Output: 0

Example 2: Using Number Methods

This example shows how to use various methods available on the Number object.

TypeScript Code (src/index.ts)

let num: number = 123.456;

// toFixed
let fixed = num.toFixed(2);
console.log(`Fixed: ${fixed}`); // Output: 123.46

// toExponential
let exponential = num.toExponential(2);
console.log(`Exponential: ${exponential}`); // Output: 1.23e+2

// toPrecision
let precision = num.toPrecision(4);
console.log(`Precision: ${precision}`); // Output: 123.5

Compiling to JavaScript

To compile the TypeScript code to JavaScript, run the TypeScript compiler:

tsc src/index.ts

Output in JavaScript (src/index.js)

// Basic Number Operations
var a = 10;
var b = 5;

var addition = a + b;
var subtraction = a - b;
var multiplication = a * b;
var division = a / b;
var modulus = a % b;

console.log("Addition: " + addition);       // Output: 15
console.log("Subtraction: " + subtraction); // Output: 5
console.log("Multiplication: " + multiplication); // Output: 50
console.log("Division: " + division);       // Output: 2
console.log("Modulus: " + modulus);         // Output: 0

// Using Number Methods
var num = 123.456;

// toFixed
var fixed = num.toFixed(2);
console.log("Fixed: " + fixed); // Output: 123.46

// toExponential
var exponential = num.toExponential(2);
console.log("Exponential: " + exponential); // Output: 1.23e+2

// toPrecision
var precision = num.toPrecision(4);
console.log("Precision: " + precision); // Output: 123.5

Running the JavaScript

To see the output of the compiled JavaScript code, run the JavaScript file using Node.js:

node src/index.js

Conclusion

In this chapter, we covered the number type in TypeScript, including how to declare and use numeric variables, perform basic arithmetic operations, and use various number methods. We provided examples with their outputs to illustrate how numbers work in TypeScript. Understanding the number type is essential for performing calculations and handling numeric data in your TypeScript programs.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare