---
title: "js 保留n为小数"
author: "jarod2008"
date: "2009-03-31T18:07:39Z"
tags: ["prototype"]
slug: "js-n"
draft: false
lastmod: "2009-03-31T18:07:39Z"
id: 358504
url: "https://jarod2008.iteye.com/blog/358504"
---

Number.prototype.toFixed=function(len)//精确小数    
  {    
  if(isNaN(len)||len==null)    
  {    
  len   =   0;    
  }    
  else    
  {    
  if(len&lt;0)    
  {    
  len   =   0;    
  }    
  }    
  return   Math.round(this   *   Math.pow(10,len))   /   Math.pow(10,len);    
  }    
     
    var   t=parseFloat(form1.text2.value)\*parseFloat(form1.text3.value);    
    form1.text1.value=t.toFixed(2)