The trunc() function will remove the decimal part of the number and keep only the integer part. Its execution logic is very simple. It just deletes the decimal part and decimal point of the number, regardless of whether the parameter is a positive number or a negative number. Let's take a look at the specific usage of the trunc() function.
Let’s first take a look at the basic syntax of the trunc() function
Math.trunc(value)
value represents any number.
Note: The parameters passed into this method will be implicitly converted to numeric types.
Let’s look at the specific examples
The code is as follows
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <script type="text/javascript"> // 正数浮点类型作为参数传递时 document.write(Math.trunc(15.56)+"<br/>"); // 负数浮点类型作为参数传递时 document.write(Math.trunc(-15.56)+"<br/>"); // 0到1之间的正数作为参数传递时 document.write(Math.trunc(0.236)+"<br/>"); // 0到-1之间的负数作为参数传递时 document.write(Math.trunc(-0.236)); </script> </script> </body> </html>
The running results are as follows
15 -15 0 0
This article ends here That’s all over. For more exciting content, you can pay attention to other related column tutorials on the PHP Chinese website! ! !
The above is the detailed content of How to use trunc function. For more information, please follow other related articles on the PHP Chinese website!