将数字字符串格式化为指定长度
生活随笔
收集整理的這篇文章主要介紹了
将数字字符串格式化为指定长度
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
將數(shù)字字符串格式化為指定長度的JavaScript函數(shù)
function formatNum(number,len){var strLength = len - number.length;//格式長度減去數(shù)字的長度,就是數(shù)字前補"0"的個數(shù)for(var i=0; i<strLength;i++){number = "0"+number;}return number;} **判斷和改變長度** function convert(){var number = document.getElementById("number").value;var num_len = document.getElementById("num_len").value;if(number==""){alert("請輸入要格式化的數(shù)字!");document.getElementById("number").focus();return;}if(isNaN(number)){alert("您輸入的數(shù)字不正確!");document.getElementById("number").focus();return;}if(num_len==""){alert("請輸入格式化后字符串的長度!");document.getElementById("num_len").focus();return;}if(isNaN(number)){alert("您輸入的格式化字符串的長度不正確!");document.getElementById("num_len").focus();return;}document.getElementById("convertStr").value = formatNum(number,num_len); } <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>將數(shù)字字符串格式化為指定長度</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><script language="javascript"> /***格式化數(shù)字*@number :要格式化的數(shù)字*@len:格式長度 *@return :返回格式化后的數(shù)字*/function formatNum(number,len){var strLength = len - number.length;//格式長度減去數(shù)字的長度,就是數(shù)字前補"0"的個數(shù)for(var i=0; i<strLength;i++){number = "0"+number;}return number;}function convert(){var number = document.getElementById("number").value;var num_len = document.getElementById("num_len").value;if(number==""){alert("請輸入要格式化的數(shù)字!");document.getElementById("number").focus();return;}if(isNaN(number)){alert("您輸入的數(shù)字不正確!");document.getElementById("number").focus();return;}if(num_len==""){alert("請輸入格式化后字符串的長度!");document.getElementById("num_len").focus();return;}if(isNaN(number)){alert("您輸入的格式化字符串的長度不正確!");document.getElementById("num_len").focus();return;}document.getElementById("convertStr").value = formatNum(number,num_len);}</script><style type="text/css">table{font-size: 13px;font-family: 楷體;color:navy;}input{font-size: 13px;font-family: 楷體;color:navy;}font{font-size: 12px;font-family: 楷體;color:orangered;}.style1{width: 500px;height: 160px;}</style></head><body><fieldset class="style1"><legend>數(shù)字字符串格式化</legend><form action="" id="myform"><table align="center"><tr><td>請輸入要格式化的數(shù)字:</td></tr> <tr><td><input type="text" id="number"> </td></tr><tr><td>請輸入格式化后字符串的長度:</td></tr> <tr><td><input type="text" id="num_len"/></td></tr><tr><td>格式化后的字符串:</td></tr> <tr><td><input type="text" id="convertStr"/></td></tr> <tr><td align="center"><input type="button" value="轉 換" "convert()"></td></tr></table></form></fieldset></body> </html>總結
以上是生活随笔為你收集整理的将数字字符串格式化为指定长度的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 去掉字符串左右的空格
- 下一篇: 限制Textarea文本域内容的长度