Home  >  Article  >  Web Front-end  >  How to use trunc function

How to use trunc function

不言
不言Original
2019-02-15 11:32:2213259browse

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)

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!

Statement:
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
Previous article:How to use sign functionNext article:How to use sign function