js除法四舍五入保留小数点后两位写法

js除法四舍五入保留小数点后两位写法原文连接<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">  <html>  <head> <title>floatDecimal.html</title> <metahttp-equiv="keywords"co

大家好,又见面了,我是你们的朋友全栈君。

原文连接

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
<html>    
<head>  
<title>floatDecimal.html</title>  
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">      
<meta http-equiv="description" content="this is my page">  
<meta http-equiv="content-type" content="text/html; charset=UTF-8">   
<script type="text/javascript">   
//保留两位小数    
//功能:将浮点数四舍五入,取小数点后2位     
function toDecimal(x) {   
  var f = parseFloat(x);    
if (isNaN(f)) {   
  return;    
}          
f = Math.round(x*100)/100;  
return f;        
}       
//制保留2位小数,如:2,会在2后面补上00.即2.00          

function toDecimal2(x) {
var f = parseFloat(x);      
if (isNaN(f)) {   
 return false;     
}          
var f = Math.round(x*100)/100;  
var s = f.toString();       
var rs = s.indexOf('.');      
if (rs < 0) {   
 rs = s.length;      
 s += '.';   
            }       
while (s.length <= rs + 2) {   
 s += '0';       
}            
return s;   
}                     

function fomatFloat(src,pos){      
             return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);     
}          

//四舍五入   
alert("保留2位小数:" + toDecimal(3.14159267));  
alert("强制保留2位小数:" + toDecimal2(3.14159267));
alert("保留2位小数:" + toDecimal(3.14559267));   
alert("强制保留2位小数:" + toDecimal2(3.15159267));   
alert("保留2位小数:" + fomatFloat(3.14559267, 2));
alert("保留1位小数:" + fomatFloat(3.15159267, 1));   
//五舍六入    
alert("保留2位小数:" + 1000.003.toFixed(2));          
alert("保留1位小数:" + 1000.08.toFixed(1));   
alert("保留1位小数:" + 1000.04.toFixed(1));     
alert("保留1位小数:" + 1000.05.toFixed(1));    
//科学计数  
alert(3.1415.toExponential(2));      
alert(3.1455.toExponential(2));   
alert(3.1445.toExponential(2));  
alert(3.1465.toExponential(2));  
alert(3.1665.toExponential(1));   
//精确到n位,不含n位      
alert("精确到小数点第2位" + 3.1415.toPrecision(2));   
alert("精确到小数点第3位" + 3.1465.toPrecision(3));  
alert("精确到小数点第2位" + 3.1415.toPrecision(2));  
alert("精确到小数点第2位" + 3.1455.toPrecision(2));    
alert("精确到小数点第5位" + 3.141592679287.toPrecision(5));      
</script>    
</head>
<body>
This is my HTML page. <br>   
</body> 
</html>

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/152054.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • typedef enum的用法(枚举)

    typedef enum的用法(枚举)include stdio h include string h include stdlib h intmain typedefenum saturday sunday 0 monday tuesday wednesday thursday friday workday stdlib h string h stdio h

    2025年12月9日
    7
  • php本地环境搭建教程,用 phpstudy 搭建本地 php 环境及安装 wordpress 教程「建议收藏」

    php本地环境搭建教程,用 phpstudy 搭建本地 php 环境及安装 wordpress 教程「建议收藏」很多时候我们仅仅为了测试一个项目或者临时搭建网站,用不着去vps服务器上添加,在本地搭建php环境就够了。这样就需要一个能够在windows系统中搭建本地php环境的软件包,这类软件包蛮多的,目前用的比较多的比如phpstudy,今天魏艾斯博客来介绍用phpstudy搭建本地php环境及本地安装wordpress教程。一、phpstudy下载安装。老魏建议从官方网站下…

    2022年6月16日
    45
  • python2 nonlocal_python非零返回

    python2 nonlocal_python非零返回nonlocal可以将一个变量声明为非本地变量,在python的lru_cache看到了使用defdecorator(func):a=1defwrapper(*args,**kwargs):nonlocalaa+=1returnfunc()returnwrapper实例中,当a变量是不可变类型时,因为包装函数引用了a,装饰器执行结束,在包装函数里改变a的值,需要…

    2025年9月19日
    6
  • J2EE架构师手册

    J2EE架构师手册 

    2022年6月29日
    28
  • springboot项目启动原理_spring原理和实现机制

    springboot项目启动原理_spring原理和实现机制SpringBoot启动类:@SpringBootApplicationpublicclassMySpringbootApplication{publicstaticvoidmain(String[]args){SpringApplication.run(MySpringbootApplication.class,args);}…

    2025年9月3日
    4
  • python zipfile_Python zipfile

    python zipfile_Python zipfile从简单的角度来看的话,zip格式会是个不错的选择,而且python对zip格式的支持够简单,够好用。1)简单应用如果你仅仅是希望用python来做压缩和解压缩,那么就不用去翻文档了,这里提供一个简单的用法,让你一看就能明白。importzipfilef=zipfile.ZipFile(‘filename.zip’,’w’,zipfile.ZIP_DEFLATED)f.write(‘fil…

    2025年12月15日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号