本文实例讲述了JS实现的数字格式化功能.分享给大家供大家参考,具体如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>数字格式化</title><script type="text/javascript" language="javascript" >//n为要转化的数字/数字字符串,s为后面要加的单位,可为空function num2money(n, s){ if (typeof s == "undefined") s = ""; n = parseInt(n); n = parseInt(n) + Number(((n - parseInt(n)) + "").substring(0, 3)); if (n / 1000 >= 1) { var nn = '' + n; nn = nn.substring(nn.length - 3, nn.length); s = ',' + nn + s; return num2money(parseInt(n / 1000), s); } else { return n + s; }}document.write(num2money("123.45", "美元"));//输出:123美元</script></head><body></body></html>
希望本文所述对大家JavaScript程序设计有所帮助。
新闻热点
疑难解答