DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. JavaScript
  4. How to Format a Number to a Specific Number of Decimal Places in JavaScript

How to Format a Number to a Specific Number of Decimal Places in JavaScript

Sai gowtham user avatar by
Sai gowtham
·
Jan. 09, 20 · Tutorial
Like (3)
Save
Tweet
Share
27.40K Views

Join the DZone community and get the full member experience.

Join For Free

In this tutorial, we are going to learn about formatting a number to specific decimal places in JavaScript using the toFixed() method.

Consider that we have a number like this.

JavaScript




x


 
1
const num  = 123.1390;



Now, we need to format the above number according to specific decimal places like 123.12 or 123.139.

Using toFixed() Method

The toFixed() method formats a number and returns the string representation of a number. Be default the toFixed() method removes the fractional part.

It also accepts the optional argument called digits, which means we need to specify the number of digits after the decimal point.

Let’s see an example:

JavaScript




xxxxxxxxxx
1


 
1
const num  = 123.1390
2
 
          
3
            // fractional part is removed
4
console.log(num.toFixed()); // "123"



You may also like: How JavaScript Actually Works: Part 1.

Now, you can see that our number is converted to a string representation. Because of this, we need to convert the string back to a number by adding + operator.

JavaScript




xxxxxxxxxx
1


 
1
console.log(+num.toFixed()); // 123



Formatting Number to Two Decimal places

To format a number to two decimal places we need to pass 2 as an argument to the toFixed() method.

JavaScript




xxxxxxxxxx
1


 
1
const num  = 123.1390
2
 
          
3
console.log(+num.toFixed(2)); // 123.13



Similarly, we can format a number according to our needs like this:

JavaScript




x
11


 
1
const num  = 123.1390
2

           
3
          // 1 decimal place
4
console.log(+num.toFixed(1)); // 123.1
5

           
6
          // 2 decimal places
7
console.log(+num.toFixed(2)); // 123.13
8

           
9

           
10
          // 3 decimal places
11
console.log(+num.toFixed(3)); // 123.139



Further Reading

  • Guide to New JavaScript Features Introduced at Google I/O 2019.
  • An Introduction to JavaScript/ES6 Arrow Functions.
  • Comparing React With ES5 vs. React With ES6.
JavaScript

Published at DZone with permission of Sai gowtham. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What To Know Before Implementing IIoT
  • What Is JavaScript Slice? Practical Examples and Guide
  • Top 5 Data Streaming Trends for 2023
  • Keep Your Application Secrets Secret

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: