 # JavaScript Format Number

Numbers are the most used data type in JavaScript. We use numbers in our day-to-day life for counting, calculating, etc. In programming, we use numbers for calculations, storing values, etc.

While programming, we often need to format numbers in different ways like rounding off to a certain precision, separating numbers by commas, etc.

In this article, we will learn how to format numbers in JavaScript. ## Number Formatting Introduction

In JavaScript, we do not have integer, float, or double data types for numbers like in Java, C, or C++.

Instead, JavaScript uses a Number data type which is always double. The number data type is used to represent numeric values which represent both integer and floating-point numbers.

While programming we generally face situations like converting a long float value to a precision of 2 or 3. Like 3.141592 => 3.14.

And sometimes you have to convert a general big number into a comma-separated number like 1000000000 => 1,000,000,000 or convert a number to some currency system, like 100 => \$100.

## Number formatting in JavaScript

Let's now see how to format numbers to a certain precision, comma-separated numbers, and currency format one by one with multiple examples.

### 1. Decimal Precision

While doing some complex calculations the numbers that we play with are generally in a long decimal format which is not readable. So we round these decimal points to a certain precision.

JavaScript provides a built-in method Number.toFixed() to format a number to a specified number of decimal places.

The toFixed() method takes 1 argument which is the number of decimal places to be precise.

You can also expand the number by giving a large number fixed place value.

### 2. Comma Separated Numbers

To format a number with a comma means to separate the digits of a number by a comma at every 3rd digit like 1234567890 => 1,234,567,890.

This way it becomes quite easy to read the number in terms of thousands, millions, billions, etc.

Another style of separating number is by placing comma at like 1234567890 => 1,23,45,67,890. First comma at 3rd digit, then all at every 2nd digit.

#### 2.1. Using toLocaleString() Method

These formats can be achieved by using the num.toLocaleString() method.

It accepts 2 optional arguments: locale and options.

The default value of locale is en-US.

The locale can be any of the following:

• en-US
• en-GB
• hi-IN
• ar-EG
• etc

The position of the comma can be changed by different locales like the 'en-US' separates numbers with a comma at every 3 digits while 'hi-IN' uses a comma at every 2 digits (the last 3 digits are not separated).

#### 2.2. Custom Function For Comma Separation

You can also create your own custom function that takes a number and returns a comma-separated number.

Our Javascript function will accept a number and return a comma-separated number at every 3 digits.

The steps to create the function are as follows:

1. Check if the number is decimal. If it is decimal, then take digits before the decimal point because we only need to place a comma before the decimal point.
2. Check if the number is negative. If it is negative, then remove the sign for now.
3. Create a loop to iterate through the number and place a comma at every 3 digits.
4. Add sign back if it was negative. Also, add a decimal point and decimal digits if it was decimal.
5. Finally, return the number.

#### 2.2. Comma Separation using regex

The same function can be created using the regular expression and replace method.

### 3. JavaScript Number Format Currency

JavaScript has a built-in method that can be used to format numbers for currency, it is Intl.NumberFormat().

It is part of the Intl (international) object. The Intl.NumberFormat() method creates an object that is language sensitive and can be used to format numbers for currency.

It accepts 2 optional arguments: locale (en-US, hi-IN, etc) and options({style, currency}).

The same is also achievable using the toLocaleString() method.

## Conclusion

In this article, we learned number format javascript methods. JavaScript numbers can be formatted in different ways like commas, currency, etc. You can use the toFixed() method to format the number with decimal points, and the toLocaleString() method to format the number with commas and Intl.NumberFormat() method to format the number with currency.

You can also create your own custom function to format the number. This article covers all types of number conversion.