What are the js functions that retain two decimal places?
js functions that retain two decimal places include: 1. [toFixed()] function; 2. [Math.floor()] function does not round, but rounds down; 3. Use string matching method; 4. Round to 2 decimal places; 5. Float to 2 decimal places.
js functions that retain two decimal places are:
1. Rounding related
1. toFixed() method
Please note that two decimal places are retained and the numerical type data is changed into a string type
// 1.四舍五入 var num =2.446242342; num = num.toFixed(2); console.log(num); //2.45 console.log(typeof num); // string
2. Math.floor() , do not round, round down
Note, do not change the data type
// 2.不四舍五入 向下取整 num = Math.floor(num * 100) / 100; console.log(num); //2.44 console.log(typeof num); // number
3. String matching
Note, first convert the data into a string, and finally Convert to numeric type
// 3.不四舍五入 字符串匹配再转换 num = Number(num.toString().match(/^\d+(?:\.\d{0,2})?/)); console.log(num); //2.44 console.log(typeof num); // number
4. Round and keep 2 decimal places (if the second decimal place is 0, keep one decimal place)
Note that the data type remains unchanged
//4.四舍五入保留2位小数(若第二位小数为0,则保留一位小数) function keepTwoDecimal(num) { var result = parseFloat(num); if (isNaN(result)) { alert('传递参数错误,请检查!'); return false; } result = Math.round(num * 100) / 100; return result; }; keepTwoDecimal(num); console.log(num); //2.44 console.log(typeof num); //number
5. Round to keep 2 decimal places (if there are not enough digits, use 0 as substitute)
Note that the data type changes to string type
//5.四舍五入保留2位小数(不够位数,则用0替补) function keepTwoDecimalFull(num) { var result = parseFloat(num); if (isNaN(result)) { alert('传递参数错误,请检查!'); return false; } result = Math.round(num * 100) / 100; var s_x = result.toString(); //将数字转换为字符串 var pos_decimal = s_x.indexOf('.'); //小数点的索引值 // 当整数时,pos_decimal=-1 自动补0 if (pos_decimal < 0) { pos_decimal = s_x.length; s_x += '.'; } // 当数字的长度< 小数点索引+2时,补0 while (s_x.length <= pos_decimal + 2) { s_x += '0'; } return s_x; } console.log(keepTwoDecimalFull(120.5)); //120.50 console.log(typeof keepTwoDecimalFull(120.5)); //string console.log(keepTwoDecimalFull(2.446242342)); //2.45 console.log(typeof keepTwoDecimalFull(2.446242342)); //string
2. Keep two decimal places for floating point numbers decimal places
1. Round the floating point number to 2 decimal places
Note that the data type remains unchanged
//浮点数保留两位小数 //1.功能:将浮点数四舍五入,取小数点后2位 function toDecimal(x) { var f = parseFloat(x); if (isNaN(f)) { return; } f = Math.round(x*100)/100; return f; } console.log(toDecimal(3.1465926)); // 3.15 console.log(typeof toDecimal(3.1415926)); //number
2. Force 2 decimal places to be retained. For example: 2, 00 will be added after 2. That is, 2.00
Note that the data type changes to string type
//2.强制保留2位小数,如:2,会在2后面补上00.即2.00 function toDecimal2(x) { var f = parseFloat(x); if (isNaN(f)) { return false; } var f = Math.round(x*100)/100; var s = f.toString(); var rs = s.indexOf('.'); if (rs < 0) { rs = s.length; s += '.'; } while (s.length <= rs + 2) { s += '0'; } return s; } console.log(toDecimal2(3.1)); // 3.10 console.log(typeof toDecimal2(3.1415926)); //string
3. Keep two decimal places and the floating point number will not be filled if there are not enough rounding digits. 0
Note that the data type remains unchanged
// 3.保留两位小数 浮点数四舍五入 位数不够 不补0 function fomatFloat(src,pos){ return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos); } console.log(fomatFloat(3.12645,2)); // 3.13 console.log(typeof fomatFloat(3.1415926)); //numbe
More related free learning recommendations: javascript video tutorial
The above is the detailed content of What are the js functions that retain two decimal places?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
