🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment