惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
博客园 - 司徒正美
月光博客
月光博客
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
Jina AI
Jina AI
GbyAI
GbyAI
Y
Y Combinator Blog
罗磊的独立博客
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
量子位
雷峰网
雷峰网
博客园 - 【当耐特】
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The GitHub Blog
The GitHub Blog
S
Secure Thoughts
博客园 - 三生石上(FineUI控件)
Cyberwarzone
Cyberwarzone
NISL@THU
NISL@THU
J
Java Code Geeks
C
Cisco Blogs
人人都是产品经理
人人都是产品经理
Webroot Blog
Webroot Blog
腾讯CDC
博客园 - 叶小钗
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Troy Hunt's Blog
AI
AI
L
LangChain Blog
Know Your Adversary
Know Your Adversary
T
Tenable Blog
M
MIT News - Artificial intelligence
P
Privacy & Cybersecurity Law Blog
L
LINUX DO - 最新话题
Hugging Face - Blog
Hugging Face - Blog
F
Full Disclosure
P
Proofpoint News Feed
AWS News Blog
AWS News Blog
有赞技术团队
有赞技术团队
A
Arctic Wolf
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Schneier on Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
W
WeLiveSecurity
The Cloudflare Blog

博客园 - ms_dos

数据库中char(13)+char(10)转换为页面的换行 System.DBNull的用法 CustomValidator验证CheckBoxList必须选择且只能选择一个 GridView编辑状态下DropDownList的联动 - ms_dos - 博客园 GridView单元格换行 - ms_dos - 博客园 js实现indexOf - ms_dos - 博客园 AspxCallBack控件的CallBack事件 HyperLink控件邦定参数 - ms_dos - 博客园 CSS实现圆角DIV - ms_dos - 博客园 值得收藏的22个搜索下载免费PDF电子书的网站 游标的使用方法 [转]Sql Server遍历表记录 将页面滚动条置于顶端 div居中 - ms_dos - 博客园 ResolveUrl的用法 - ms_dos - 博客园 窗口全屏 JS实现一行内多列DIV同高 - ms_dos - 博客园 动态显示男女性别的两种方法 OnClientClick属性中的换行符 - ms_dos - 博客园
js实现精确到小数点后几位的四舍五入函数
ms_dos · 2010-08-27 · via 博客园 - ms_dos

js中可以使用Math.round实现整数的四舍五入,如果需要实现精确到小数点多少位则需要编写自定义函数。

方法一:

function round(v,e)
{
var t=1;
for(;e>0;t*=10,e--);
for(;e<0;t/=10,e++);
return Math.round(v*t)/t;
}

参数里的: v表示要转换的值 e表示要保留的位数

注意:如果使用用户控件,该js不能写在ascx文件内,应该写在使用该用户控件的aspx文件内。


方法二:

function FormatNumber(srcStr,nAfterDot){
var srcStr,nAfterDot;
var resultStr,nTen;
srcStr = ""+srcStr+"";
strLen = srcStr.length;
dotPos = srcStr.indexOf(".",0);
if (dotPos == -1){
resultStr = srcStr+".";
for (i=0;i<nAfterDot;i++){
resultStr = resultStr+"0";
}
}
else{
if ((strLen - dotPos - 1) >= nAfterDot){
nAfter = dotPos + nAfterDot + 1;
nTen =1;
for(j=0;j<nAfterDot;j++){
nTen = nTen*10;
}
resultStr = Math.round(parseFloat(srcStr)*nTen)/nTen;
}
else{
resultStr = srcStr;
for (i=0;i<(nAfterDot - strLen + dotPos + 1);i++){
resultStr = resultStr+"0";
}
}
}

return resultStr;
}