🎓 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
In this chapter, we will explore the Math.floor() method in TypeScript. This method returns the largest integer less than or equal to a given number. Understanding how to use Math.floor() is useful for rounding numbers down to the nearest integer.
Table of Contents
- Definition
- Syntax
- Examples
- Conclusion
1. Definition
The Math.floor() method returns the largest integer less than or equal to a given number. This method always rounds a number down to the nearest integer.
2. Syntax
Math.floor(x);
Parameters
x: A number to be rounded down.
Return Value
The method returns the largest integer less than or equal to the given number.
3. Examples
Let's look at some examples to understand how Math.floor() works in TypeScript.
Example 1: Basic Usage
In this example, we use Math.floor() to round a positive decimal number down to the nearest integer.
let num: number = 4.7;
let result = Math.floor(num);
console.log(result); // Output: 4
Example 2: Rounding a Negative Decimal Number
In this example, we use Math.floor() to round a negative decimal number down to the nearest integer.
let num: number = -4.7;
let result = Math.floor(num);
console.log(result); // Output: -5
Example 3: Rounding Whole Numbers
In this example, we use Math.floor() to round whole numbers. Whole numbers remain unchanged.
let num1: number = 5;
let num2: number = -5;
console.log(Math.floor(num1)); // Output: 5
console.log(Math.floor(num2)); // Output: -5
Example 4: Rounding a Positive Decimal Number Less Than .5
In this example, we use Math.floor() to round a positive decimal number less than .5 down to the nearest integer.
let num: number = 4.3;
let result = Math.floor(num);
console.log(result); // Output: 4
Example 5: Using Math.floor() with Expressions
In this example, we use Math.floor() with expressions to round the result down to the nearest integer.
let num1: number = 3.7;
let num2: number = 2.2;
let result = Math.floor(num1 + num2);
console.log(result); // Output: 5
Example 6: Rounding Small Positive Decimal Numbers
In this example, we use Math.floor() to round small positive decimal numbers down to the nearest integer.
let num: number = 0.999;
let result = Math.floor(num);
console.log(result); // Output: 0
4. Conclusion
In this chapter, we explored the Math.floor() method in TypeScript, which is used to return the largest integer less than or equal to a given number. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use Math.floor() effectively can help in various mathematical calculations and scenarios where rounding numbers down is required.
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