本文實例講述了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程序設計有所幫助。
新聞熱點
疑難解答