# How to Round a Number to 2 Decimal Places in JavaScript

Last updated on December 11, 2022 To round a number to 2 decimal places in JavaScript, call the `toFixed()` method on the number, i.e., `num.toFixed(2)`. `toFixed()` will round and format the number to 2 decimal places.

For example:

``````const num = 5.3281;

const result = num.toFixed(2);

console.log(result); // 5.33

const num2 = 3.1417

const result2 = num2.toFixed(2);

console.log(result2); // 3.14
``````

The `toFixed()` method takes a number `F` and returns a string representation of a number with `F` number of digits after the decimal points. `F` here is determined by the first argument passed to `toFixed()`, the `fractionDigits` argument.

``````const num = 5.3281;

console.log(num.toFixed(0)); // 5

console.log(num.toFixed(1)); // 5.3

console.log(num.toFixed(2)); // 5.33

console.log(num.toFixed(3)); // 5.328

console.log(num.toFixed(4)); // 5.3281

console.log(num.toFixed(5)); // 5.32810
``````

## 11 Amazing New Features in ES13 Get up to speed with all the latest features added in ECMAScript 13 to modernize your JavaScript with shorter and more expressive code.

## Parse result of `toFixed()` to number

Remember, `toFixed()` returns a string representation:

``````const num = 5.3281;

const result = num.toFixed(2);

console.log(result); // 5.33

console.log(typeof result); // string
``````

But we can always convert the result to a number with the `Number()` constructor:

``````const num = 5.3281;

const result = Number(num.toFixed(2));

console.log(result); // 5.33

console.log(typeof result); // number
``````

If the string has trailing zeroes, they will be removed in the conversion:

``````const num = 9.999999;

const strResult = num.toFixed(2);

const result = Number(strResult);

console.log(strResult); //10.00

console.log(result); // 10
``````

The trailing zeros after the decimal point don't change the value of a number, so `10.00` is the same as `10` or `10.00000000`

``````console.log(10.00 === 10); // true

console.log(10.00000000 == 10); // true``````

## Round decimal string to 2 decimal places

Sometimes the input might be stored as a string. In this case, we'll first need to convert the number to a float with the `parseFloat()` function before using `toFixed()` to round it to 2 decimal places.

For example:

``````const numStr = '17.23593';

// 👇 convert string to float with parseFloat()
const num = parseFloat(numStr);

const result = num.toFixed(2); // 17.24

console.log(result);
``````

Not all decimal numbers can be represented precisely in binary, so there are some rounding errors in JavaScript's floating-point number system. For example:

``````console.log(44.85 * 0.1); // 4.485

console.log(45.85 * 0.1); // 4.585

console.log(46.85 * 0.1); // 4.6850000000000005 (?)
``````

In this example, `46.85 x 0.1` equals `4.6850000000000005` because `46.85` can't be accurately represented in binary floating-point format.

``````console.log((1.415).toFixed(2)); // 1.42

console.log((1.215).toFixed(2)); // 1.22

console.log((1.015).toFixed(2)); // 1.01 (?)``````

Like in the first one, here `1.015` is rounded down to 2 decimal places as `1.01` instead of `1.02`, because `1.015` also can't be represented accurately in the binary number system.

One of the most popular examples of this flaw is the classic `0.1 + 0.2`:

``````console.log(0.1 + 0.2 === 0.3); // false

console.log(0.1 + 0.2); // 0.30000000000000004
``````

You can find this and more of other interesting JavaScript quirks in our Crazy JS Book.

## Every Crazy Thing JavaScript Does Avoid painful bugs and save valuable time with Every Crazy Thing JavaScript Does, a captivating guide to the subtle caveats and lesser-known parts of JavaScript.
{"ft_lang_label":"__label__en","ft_lang_prob":0.5134942,"math_prob":0.99330753,"size":3133,"snap":"2023-40-2023-50","text_gpt3_token_len":899,"char_repetition_ratio":0.22563119,"word_repetition_ratio":0.05934066,"special_character_ratio":0.35588893,"punctuation_ratio":0.232493,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.988084,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-04T22:07:43Z\",\"WARC-Record-ID\":\"<urn:uuid:d92aeed6-611b-4213-834d-4493365dc1c3>\",\"Content-Length\":\"69504\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:777e3d85-7768-4b90-8824-653a4e0defa7>\",\"WARC-Concurrent-To\":\"<urn:uuid:5f29b6c9-6374-43e7-90f3-f145227c8252>\",\"WARC-IP-Address\":\"199.36.158.100\",\"WARC-Target-URI\":\"https://www.codingbeautydev.com/blog/javascript-round-number-to-2-decimal-places\",\"WARC-Payload-Digest\":\"sha1:YI7FUH5VRFUVZEDRT4UJGKXH44JCKHC3\",\"WARC-Block-Digest\":\"sha1:QDJA5EIHAO4S2NZI54TOGY3K7EE3NFRF\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100535.26_warc_CC-MAIN-20231204214708-20231205004708-00187.warc.gz\"}"}