How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Format a Number with Decimals in JavaScript
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Format a Number with Decimals in JavaScript
Web Development

How to Format a Number with Decimals in JavaScript

how7o
By how7o
Last updated: May 22, 2026
5 Min Read
JavaScript format number with decimals — toFixed, Math.floor, and Intl.NumberFormat
SHARE

To format a number with decimals in JavaScript, use the built-in toFixed method: (3.10391).toFixed(3) returns the string '3.104'. This guide covers toFixed, when it isn’t enough (truncation, locales, thousands separators), and the floating-point precision gotcha that catches people the first time.

Contents
  • TL;DR
  • The toFixed method
  • Round, floor, or ceil to N decimals
  • The floating-point precision gotcha
  • Locale-aware formatting
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 in Chromium, Firefox, Safari, and Node.js 22. Originally published 2023-03-19, rewritten and updated 2026-05-17.

TL;DR

(3.10391).toFixed(3);                     // "3.104"  (rounded)
parseFloat("3.10391").toFixed(3);          // "3.104"  (string in -> rounded string out)

// Need a number back?
Number((3.10391).toFixed(3));              // 3.104

// Need truncation, not rounding?
Math.floor(3.10391 * 1000) / 1000;         // 3.103

// Locale-aware + thousands separator?
new Intl.NumberFormat('en-US',
  { minimumFractionDigits: 3,
    maximumFractionDigits: 3 }
).format(1234567.891);                     // "1,234,567.891"

The toFixed method

toFixed(n) is a method on every JavaScript number. It returns a string representation rounded to n decimal places:

(3.10391).toFixed(3);     // "3.104"
(3.10391).toFixed(2);     // "3.10"
(3.10391).toFixed(0);     // "3"
(3.5).toFixed(0);         // "4"

If the value comes from a database or API as a string, wrap it in parseFloat first, since toFixed only exists on numbers:

const raw = "3.10391";       // e.g. from JSON
parseFloat(raw).toFixed(3);  // "3.104"
JavaScript format number with decimals — toFixed, Math.floor, and Intl.NumberFormat compared

Round, floor, or ceil to N decimals

toFixed always rounds. If you specifically need truncation or ceiling, combine Math.floor / Math.ceil with a multiplier:

// Round (same as toFixed)
(Math.round(3.10391 * 1000) / 1000).toFixed(3);  // "3.104"

// Truncate
(Math.floor(3.10391 * 1000) / 1000).toFixed(3);  // "3.103"

// Ceiling
(Math.ceil(3.10391 * 1000) / 1000).toFixed(3);   // "3.104"

The * 1000 / 1000 shift is what controls the precision — three decimals here. For two decimals use * 100 / 100, for four use * 10000 / 10000.

The floating-point precision gotcha

JavaScript numbers are IEEE-754 floats, so 0.1 + 0.2 is internally 0.30000000000000004 — not 0.3. toFixed(2) rounds the display to '0.30', which is what you usually want for display. But strict equality (===) still fails:

0.1 + 0.2 === 0.3;                  // false
(0.1 + 0.2).toFixed(2);             // "0.30"
Number((0.1 + 0.2).toFixed(2)) === 0.30;  // true

For display, toFixed is fine. For money or any value where every cent matters, store integers (cents/satoshis) or use a decimal library like decimal.js — never trust the float for accounting.

Locale-aware formatting

toFixed always uses . as the decimal separator and never adds thousands separators. For user-facing display, Intl.NumberFormat handles both, in any locale:

const fmt = new Intl.NumberFormat('en-US', {
  minimumFractionDigits: 3,
  maximumFractionDigits: 3,
});
fmt.format(1234567.891);            // "1,234,567.891"

new Intl.NumberFormat('fr-FR',
  { minimumFractionDigits: 3,
    maximumFractionDigits: 3 }
).format(1234567.891);              // "1 234 567,891"

Built into every modern browser and Node.js — no dependency needed.

Frequently asked questions

Does toFixed return a string or a number?

A string. (3.10391).toFixed(3) returns '3.104', not the number 3.104. That’s fine for display, but if you need a number for further math, wrap with Number(...) or parseFloat(...): Number((3.10391).toFixed(3)).

Does toFixed round or truncate?

It rounds, using banker’s rounding in most modern engines (round half to even). (2.5).toFixed(0) returns '3', (0.125).toFixed(2) returns '0.13'. If you specifically need truncation (drop the extra digits without rounding up), use Math.floor(value * 10**n) / 10**n instead.

Why does (0.1 + 0.2).toFixed(2) return '0.30' but 0.1 + 0.2 === 0.3 is false?

Floating-point precision. 0.1 + 0.2 in IEEE-754 is actually 0.30000000000000004 — strict equality with 0.3 fails. toFixed(2) rounds the display to '0.30', which looks correct. For money or any value where precision matters, store integers (cents) or use a decimal library; toFixed is for display only.

How do I add thousands separators?

toFixed doesn’t do separators. Use Intl.NumberFormat instead: new Intl.NumberFormat('en-US', { minimumFractionDigits: 3, maximumFractionDigits: 3 }).format(1234567.891) returns '1,234,567.891'. It’s locale-aware, so French users see '1 234 567,891'.

Should I use toFixed, Intl.NumberFormat, or a library?

For one-off display in a single locale, toFixed is fine. For anything user-facing with locales or currency, Intl.NumberFormat is the right choice — built into every modern browser and Node.js, no dependencies. Pull in a library (decimal.js, big.js) only when you need exact decimal arithmetic for accounting.

Related guides

  • How to Check if a JavaScript String Is a Valid URL
  • How to Convert a String to Float in PHP

References

MDN Number.prototype.toFixed: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed. Intl.NumberFormat: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat.

TAGGED:configurationJavaScript

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Check Pure-FTPd installed on Linux — rpm and dpkg package queries How to Check if Pure-FTPd Is Installed on Linux
Next Article Nginx 502 with recv() failed 104 connection reset — PHP-FPM timeout and memory tuning Fix Nginx ‘recv() failed (104: Connection reset by peer)’ with FastCGI
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026

You Might Also Like

MariaDB 11.4 LTS database cylinder being installed on Ubuntu
Server Management

How to Install MariaDB 11.4 LTS on Ubuntu 24.04 (Fresh Install, Secured)

10 Min Read
XAMPP autostart Apache and MySQL on Windows — XAMPP Control Panel and services
OS

How to Auto-start Apache and MySQL with XAMPP on Windows

5 Min Read
Linux add and delete users from the terminal — adduser, passwd, sudo group, userdel
Server Management

How to Add and Delete Users on a Linux Server from the Terminal

7 Min Read
Capitalize all words in JavaScript with a ucwords-style function
Web Development

Capitalize All Words in JavaScript (ucwords Equivalent) + First Letter Uppercase

6 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?