🎓 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 toFixed() method for numbers in TypeScript. This method formats a number using fixed-point notation. Understanding how to use toFixed() is useful for formatting numbers to a specified number of decimal places.
Table of Contents
- Definition
- Syntax
- Examples
- Conclusion
1. Definition
The toFixed() method formats a number using fixed-point notation. This method returns a string representation of the number with a specified number of digits after the decimal point.
2. Syntax
number.toFixed(digits?);
Parameters
digits(optional): An integer specifying the number of digits to appear after the decimal point. It must be in the range of 0 to 20, inclusive. If omitted, the default is 0.
Return Value
The method returns a string representing the number in fixed-point notation.
3. Examples
Let's look at some examples to understand how toFixed() works in TypeScript.
Example 1: Basic Usage
In this example, we use toFixed() to format a number with no decimal places.
let num: number = 123.456;
let result = num.toFixed();
console.log(result); // Output: "123"
Example 2: Specifying Decimal Places
In this example, we use toFixed() to format a number with two decimal places.
let num: number = 123.456;
let result = num.toFixed(2);
console.log(result); // Output: "123.46"
Example 3: Formatting with More Decimal Places
In this example, we use toFixed() to format a number with five decimal places.
let num: number = 123.456;
let result = num.toFixed(5);
console.log(result); // Output: "123.45600"
Example 4: Using toFixed() with Small Numbers
In this example, we use toFixed() to format a small number with three decimal places.
let num: number = 0.000123;
let result = num.toFixed(3);
console.log(result); // Output: "0.000"
Example 5: Using toFixed() with Large Numbers
In this example, we use toFixed() to format a large number with two decimal places.
let num: number = 987654321.123;
let result = num.toFixed(2);
console.log(result); // Output: "987654321.12"
Example 6: Rounding Behavior
In this example, we use toFixed() to demonstrate how it handles rounding.
let num: number = 1.005;
let result = num.toFixed(2);
console.log(result); // Output: "1.01"
4. Conclusion
In this chapter, we explored the toFixed() method for numbers in TypeScript, which is used to format a number using fixed-point notation. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use toFixed() effectively can help in various tasks where precise formatting of numerical output is required, especially when dealing with currency, measurements, and other decimal-based data.
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