Get the App
SLTechnology News&Howtos  ›  Development  › 

How to keep two decimal places in javascript

Shulou Source: shulou.com Published: 2022-06-02 01:21:38 10月03日 Update

This article is about how to keep two decimal places in javascript. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

In JavaScript, you can use the toFixed () method to retain two decimal places, the purpose of this method is to round the value to the specified number of decimal places, when the parameter is set to "2", the returned result will retain the specified value of two decimal places, the syntax is "numeric .tofixed (2)".

The operating environment of this tutorial: windows10 system, javascript1.8.5 version, Dell G3 computer.

What is the method of keeping two decimal places in javascript

The toFixed () method rounds Number to a specified number of decimal places.

The syntax is:

Number.toFixed (x)

Where x is required. The specified number of decimal places is a value between 0 and 20, including 0 and 20, and some implementations can support a wider range of values. If this parameter is omitted, 0 will be used instead.

The returned result has a fixed x-digit after the decimal point.

Examples are as follows:

123 Click the button to display the value of the reserved decimal

Tags: Decimals numeric values methods digits numbers results rounding content parameters more articles grammar quad good practical in between action decimal point that is buttons Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MariaDB MySQL OPPO Reno Docker