How to round js values
1.toFixed() method can round Number to a number with specified decimal places.
NumberObject.toFixed(num)
num must be written, specifying the number of decimal places, which is a value between 0 ~ 20, including 0 and 20. Some implementations can support a larger range of values. If this parameter is omitted, 0 will be used instead.
当num超过20的时候,js会出错,这东西好像只能传一个数字进去,字符串会爆不是一个方法
Bug in the method:
<span style="color:#33cc00">Number(13.35).toFixed(1); //13.3 <br>Number(0.055).toFixed(1); //0.1<br>原因:<span style="font-size:13.3333px; font-family:PingFangSC-Regular,Verdana,Arial,微软雅黑,宋体">原生toFixed(x)截取小数的时候会有误差</span><br>//如果要修改这个缺陷,可以把js中的number类型的tofixed方法重写。</span>
2.Math.round() Method rounds a number to the nearest integer.
Math.round(x)
x is the value to be calculated. This method returns the nearest integer to the given numeric expression.
Existing bug
##
<span style="background-color:rgb(255,255,255)"><span style="color:#009900">Math.round(num * Math.pow(10, 2)) / Math.pow(10, 2); //num是待处理数字</span></span><span style="color:rgb(0,153,0); font-size:13.3333px; font-family:PingFangSC-Regular,Verdana,Arial,微软雅黑,宋体; background-color:rgb(255,255,255)">Math.pow(10, 2)=100</span><span style="background-color:rgb(255,255,255)"><span style="color:#009900"><br>当num = 10.10500时,计算上述表达式可得10.11。(正常)<br>当num = "10.50000"时(注意这里是字符串),计算上述表达式可得10.5。(damn it,小数点后位数不对!)</span></span>
原因:当num是字符串,进行乘法操作时,进行了类型转换,后缀零被丢弃了,导致位数不足,这个时候我们就应该进行补0
Solution:
<span style="background-color:rgb(255,255,255)"><span style="color:#003300">var iTofixed =function(num,fractionDigits) {<br> return (Math.round(num*Math.pow(10,fractionDigits))/Math.pow(10,fractionDigits)+Math.pow(10,-(fractionDigits+1))).toString().slice(0, -1)<br>};<br>iTofixed('13.5000',3);<br>//"13.500"</span></span>
The above is the detailed content of How to round js values. 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

This article will explain in detail the PHP floating point number rounding method. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP Floating Point Rounding Overview Floating point numbers are represented in computers as a decimal point followed by an exponent, however, they are often stored in approximations with a limited number of digits. When you need to round a floating point number to a specific precision, there are several ways to do it. Method 1. round() function The round() function rounds a floating point number to the nearest integer. It accepts floating point numbers and optional precision parameters. For example: $num=1.55;echoround($num);//Output: 2echoround($num,1)

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).
