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

推荐订阅源

WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Cloudbric
Cloudbric
P
Palo Alto Networks Blog
T
Threatpost
T
Tor Project blog
T
Tenable Blog
AWS News Blog
AWS News Blog
Project Zero
Project Zero
L
LangChain Blog
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Latest
Security Latest
云风的 BLOG
云风的 BLOG
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
MongoDB | Blog
MongoDB | Blog
aimingoo的专栏
aimingoo的专栏
K
Kaspersky official blog
Jina AI
Jina AI
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
D
DataBreaches.Net
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
IT之家
IT之家
Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog

博客园 - John Liu

C#中处理SQL Server中的空的DateTime型字段 推荐一个不错的国外html5模板网站 SQL SERVERR中未公开的存储过程sp_MSforeachtable - John Liu .net 4.0 ValidateRequest="false" 无效 - John Liu 《数据库设计指南》学习笔记一:设计数据库之前 巧用case when 解决多条件模糊查询问题 安装vs2010后新建项目FrameWork版本选择只有4.0的解决方案 SQL Server直接执行.sql文件 【转自大家论坛】jQuery 1.4: 15个你应该知道的新特性 - John Liu 小技巧:Response.Redirect(...,true/false) [转]100多个很有用的JavaScript函数以及基础写法大集合 思维导图,相见恨晚 jQuery的图像裁剪插件Jcrop的简单使用 微软提供的web.config文件的加密方式,掩耳盗铃! 开始专注数据库:动态Sql,自定义函数 将sql server表中的数据导出为inert into语句的形式 js进行图片的等比缩放(转自yayayaangel的百度空间) - John Liu - 博客园 web.config文件加密 - John Liu - 博客园 使用sql语句建立与删除链接服务器及执行数据库操作
金额数字的格式化(转自mimimo的百度空间)
John Liu · 2009-04-05 · via 博客园 - John Liu

     我们在网站开发中经常要对数字进行格式化,以前自己也写过一个格式化数字的js函数,不过感觉功能比较简单,今天在mimimo的空间上看到他的一篇文章,里面的函数写的不错,记录如下:
原文地址:http://hi.baidu.com/mimimo/blog/item/627db4357cf7058ca61e123c.html

     例如:
12345
格式化为12,345.00
12345.6
格式化为12,345.60
12345.67
格式化为12,345.67
只留两位小数。

回来后写了个格式化函数。可以控制小数位数,自动四舍五入。

代码如下:
function fmoney(s, n)
{
   n = n > 0 && n <= 20 ? n : 2;
   s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
   var l = s.split(".")[0].split("").reverse(),
   r = s.split(".")[1];
   t = "";
   for(i = 0; i < l.length; i ++ )
   {
      t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
   }
   return t.split("").reverse().join("") + "." + r;
}
调用:fmoney("12345.675910", 3),返回12,345.676

还原函数:
function rmoney(s)
{
   return parseFloat(s.replace(/[^\d\.-]/g, ""));
}

示例(可保存一下代码为html文件,运行查看效果):
<SCRIPT>
function fmoney(s, n)
{
   n = n > 0 && n <= 20 ? n : 2;
   s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
   var l = s.split(".")[0].split("").reverse(),
   r = s.split(".")[1];
   t = "";
   for(i = 0; i < l.length; i ++ )
   {
      t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
   }
   return t.split("").reverse().join("") + "." + r;
}
function rmoney(s)
{
   return parseFloat(s.replace(/[^\d\.-]/g, ""));
}
function g(id)
{
   return document.getElementById(id);
}
window.onload = function()
{
   var num,
   txt = g("txt"),
   txt2 = g("txt2"),
   btn = g("btn"),
   btn2 = g("btn2"),
   span = g("span");
   btn.onclick = function()
   {
      num = parseInt(g("num").value);
      txt.value = fmoney(txt.value, num);
      txt2.value = fmoney(txt2.value, num);
   }
   ;
   btn2.onclick = function()
   {
      num = parseInt(g("num").value);
      span.innerHTML = "=" + fmoney(rmoney(txt.value) + rmoney(txt2.value), num);
   }
   ;
}
;
</SCRIPT>
小数点位数:
<select id="num">
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<input type="text" id="txt" value="12345.675910"> +
<input type="text" id="txt2" value="1223"> <span id="span"></span>
<br>
<input type="button" id="btn" value="格式化">
<input type="button" id="btn2" value="相加">