Home > Web Front-end > JS Tutorial > How to use trunc function

How to use trunc function

不言
Release: 2019-02-15 11:32:22
Original
13335 people have browsed it

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.

How to use trunc function

Let’s first take a look at the basic syntax of the trunc() function

Math.trunc(value)
Copy after login

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>
Copy after login

The running results are as follows

15
-15
0
0
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template