SoFunction
Updated on 2025-04-13

Summary of the method to preserve two decimal places in JavaScript

introduction

In JavaScript, it is a common requirement to process numbers and format them to display specific decimal places. This is especially important when you need to display currency, measurements, or other data that needs to be accurate to two decimal places. This article will introduce in detail several methods to preserve two decimal places in JavaScript.

1. Use the toFixed method

toFixed is a method of the Number object in JavaScript that allows you to format numbers into strings with specified decimal places. This is the most direct way to achieve the preservation of two decimal places.

let num = 123.456789;
let formattedNum = (2); // "123.46"

NoticetoFixedThe method returns a string, not a number. If you need to convert it back to a numeric type, you can useparseFloatorNumberConstructor, but doing so may introduce floating point precision issues.

let numAsFloat = parseFloat(formattedNum); // 123.46 (but this is a floating point number, there may be accuracy issues)let numAsNumber = Number(formattedNum); // Same as above

Due to the way floating-point numbers are represented in JavaScript, you may encounter precision problems after converting back to numbers. Therefore, it is generally recommended to do this conversion only if needed, and in most cases it may be better to keep the string form.

2. Use mathematical operations

If you need to keep the number two decimal places and still use it as a number (although there may be precision problems), you can do it with math.

let num = 123.456789;
let roundedNum = (num * 100) / 100; // 123.46

Here, we first multiply the number by 100 (convert it to an integer while retaining the information of two decimal places), and then useRound it and then divide it by 100 to convert it back to the original proportion.

3. Use

For scenarios that require international digital formatting,It is a powerful tool. It allows you to format numbers based on specified locale and options.

let num = 123.456789;
let formatter = new ('en-US', {
  minimumFractionDigits: 2,
  maximumFractionDigits: 2
});
let formattedNum = (num); // "123.46"

The returned string is also a string, but it provides more flexibility and international support.

4. Custom functions (combined with the above methods)

Sometimes, you may need to create a custom function to encapsulate the above logic for reuse in your code base.

function formatToTwoDecimalPlaces(num) {
  return (2); // Return string  // Or, if you need numbers (although there are precision issues):  // return (num * 100) / 100;
}
 
let num = 123.456789;
let formattedNum = formatToTwoDecimalPlaces(num); // "123.46"

Summarize

There are multiple ways to retain two decimal places in JavaScript, each with its applicable scenarios and limitations. The toFixed method is the easiest and straightforward, but it returns a string. If you need numeric types and can accept the accuracy problems that floating point numbers may have, then using mathematical operations is an option. It is a powerful tool for scenarios that require international support. Finally, you can create a custom function to encapsulate the logic for reuse in your project.

The above is the detailed summary of the method of retaining two decimal places in JavaScript. For more information about retaining two decimal places in JavaScript, please pay attention to my other related articles!