---
title: "js几个取整函数"
author: "小丑鱼0703"
date: "2012-03-06T13:05:18Z"
tags: []
categories: ["Web前端"]
slug: "js"
draft: false
lastmod: "2015-02-09T15:15:25Z"
id: 1441509
url: "https://wangjingyi.iteye.com/blog/1441509"
---

如下几个取整函数，用来限定解的精度：

 

Math.floor()    Math对象的方法--取比当前数值小的最大整数（下取整）。

Math.ceil()      Math对象的方法--取比当前数值大的最小整数（上取整）。

Math.round()  Math对象的方法--四舍五入。

toFixed()         Number对象的方法--四舍五入保留n位小数。

                        返回 NumberObject 的字符串表示。

 

 

```
<script type="text/javascript">
   document.write(Math.round(-7.6));        //-8
   document.write(Math.ceil(-7.6));          //-7
   document.write(Math.floor(-7.6));      //-8
   var a = 12.3456
   document.write(a.toFixed(2));         //12.35
</script>
```