-
//=================================== javascript ===================================
-
-
//---- 自定义函数, 函数应该放在闭包外面, 放在闭包里面掉用不了 -----------
-
-
//substr(start[,length])表示从start位置开始取length个字符串
-
//substring(start,end)表示从start,到end之间的字符串,包括start位置的字符但是不包括end位置的字符
-
//1.start不一定就是第一个参数,end也不一定就是第二个参数,substring(3,1)时,开始位置是1,结束位置是3;
-
//2.当要返回的子字符串是从开始位置到结束时,end的值必须大于等于字符串的长度,
-
//如上边的str.substring(7,8),按照索引从0开始算的话end的最大值为7,但这边却用8,当然,使用大于8的数返回的结果也是一样的,这点比较有意思。
-
-
function dec_hex(i)
-
{
-
var s = "0123456789ABCDEF";
-
var ss = s.substr(i/16,1) + s.substr(i%16,1); //获取子字符串
-
-
return(ss);
-
}
-
-
function hex_dec(s)
-
{
-
var arg = [0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,0,10,11,12,13,14,15]; //数组
-
-
s = s.toUpperCase(s);
-
var pos1 = s.charCodeAt(0) - 48; //取字符串的ASCII
-
var pos2 = s.charCodeAt(1) - 48; //'A'的ASCII为65, -48=17, 即arg[17]=10
-
var Rtn = arg[pos1]*16 + arg[pos2];
-
-
return(Rtn);
-
}
-
-
function GetCRC8(p, Num) //p为数组传递参数, Num为数据的长度
-
{
-
var crcarray = [
-
0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
-
157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
-
35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
-
190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
-
70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
-
219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
-
101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
-
248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
-
140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
-
17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
-
175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
-
50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
-
202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
-
87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
-
233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
-
116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53];
-
var temp,CRC8 = 0;
-
-
for(var i=0; i<Num; i++)
-
CRC8 = crcarray[CRC8 ^ p[i]];
-
-
return(CRC8);
-
}
-
-
var calCRC16 = function(strSrc)
-
{
-
//strSrc = "010200C40016";
-
var len = strSrc.length;
-
var reg_crc = 0xffff;
-
var m_Crc;
-
for(var i=0 ;i <len/2;i++)
-
{
-
var c = strSrc.substr(2*i,2);
-
var r = parseInt(c,16);
-
reg_crc ^= r;
-
for(var j=0;j<8;j++)
-
{
-
m_Crc = reg_crc;
-
reg_crc>>=1;
-
if(m_Crc&0x0001)
-
{
-
reg_crc ^= 0xa001;
-
}
-
}
-
-
}
-
var temp = reg_crc.toString(16);
-
return (temp.substr(2,2)+temp.substr(0,2)).toUpperCase();
-
}
-
-
function DateToString(datetime)
-
{
-
var year = datetime.getFullYear();
-
var month = datetime.getMonth()+1; //js从0开始取
-
var date1 = datetime.getDate();
-
var hour = datetime.getHours();
-
var minutes = datetime.getMinutes();
-
var second = datetime.getSeconds();
-
-
if(month < 10){
-
month = "0" + month;
-
}
-
if(date1 < 10){
-
date1 = "0" + date1;
-
}
-
if(hour < 10){
-
hour = "0" + hour;
-
}
-
if(minutes <10){
-
minutes = "0" + minutes;
-
}
-
if(second <10){
-
second = "0" + second;
-
}
-
-
year = year.toString();
-
//year = year.substring(2, 4);
-
year = year.substr(2,2);
-
var time = year + month + date1 + hour + minutes + second; //第1个变量为string,则整个变量为string????
-
-
return(time);
-
}
-
-
//dateObj是一个日期对象,days表示给这个日期加多少天,比如说4,5(天)
-
function dateAdd(dateObj,days)
-
{
-
var tempDate = dateObj.valueOf();
-
tempDate = tempDate + days * 24 * 60 * 60 * 1000;
-
tempDate = new Date(tempDate);
-
return tempDate;
-
}
-
-
function Trim(str){
-
if(str.charAt(0) == " "){
-
str = str.slice(1);
-
str = Trim(str);
-
}
-
return str;
-
}
-
-
//判断是否是空
-
function isEmpty(pObj,errMsg){
-
var obj = eval(pObj);
-
if( obj == null || Trim(obj.value) == ""){
-
if (errMsg == null || errMsg =="")
-
alert("输入为空!");
-
else
-
alert(errMsg);
-
obj.focus();
-
return false;
-
}
-
return true;
-
}
-
-
//判断是否是数字
-
function isNumber(pObj,errMsg){
-
var obj = eval(pObj);
-
strRef = "1234567890";
-
if(!isEmpty(pObj,errMsg))return false;
-
for (i=0;i<obj.value.length;i++) {
-
tempChar= obj.value.substring(i,i+1);
-
if (strRef.indexOf(tempChar,0)==-1) {
-
if (errMsg == null || errMsg =="")
-
alert("数据不符合要求,请检查");
-
else
-
alert(errMsg);
-
if(obj.type=="text")
-
obj.focus();
-
return false;
-
}
-
}
-
return true;
-
}
-
-
function isIE()
-
{
-
if (window.navigator.userAgent.toLowerCase().indexOf("msie") >= 1)
-
return true;
-
else
-
return false;
-
}
-
-
// 返回两数之间随机数的函数
-
function rand(a, b){
-
return Math.round(a + Math.random()*(b - a));
-
}
-
-
// 定义名为each的函数,方便遍历数组或者对象的处理
-
function each(o, f){
-
if(o.length == undefined) {
-
for(var n in o) //for...in语句
-
f.call(o[n], n, o[n]);
-
}else{
-
for(var i=0, l = o.length; i<l; i++)
-
f.call(o[i], i, o[i]);
-
}
-
}
-
-
Array.prototype.each = function(callback)
-
{
-
for (i=0; i<this.length; i++) {
-
callback.call(this, this[i]);
-
}
-
}
-
var arrayObject = ["JavaScript","Prototype","Jquery"];
-
arrayObject.each(function(x){alert(x)});
-
//用prototype方法给JavaScrip的数组对象Array加一个方法each,each方法传入一个回调函数callback,
-
//callback内部根据当前调用数组对象长度循环执行callback方法,callback方法在执行时内部this就指当前调用,
-
//并传入一个参数this[i]为数组当前索引对象。上面代码运行时会顺序看到弹出对话框显示"JavaScript","Prototype","Jquery"。
-
-
//
-
//转成人民币大写金额形式
-
function RMBUpper(num)
-
{
-
var strOutput = "";
-
var strUnit = '仟佰拾亿仟佰拾万仟佰拾元角分';
-
num += "00";
-
var intPos = num.indexOf('.');
-
if (intPos >= 0)
-
num = num.substring(0, intPos) + num.substr(intPos + 1, 2);
-
strUnit = strUnit.substr(strUnit.length - num.length);
-
for (var i=0; i < num.length; i++)
-
strOutput += '零壹贰叁肆伍陆柒捌玖'.substr(num.substr(i,1),1) + strUnit.substr(i,1);
-
-
return strOutput.replace(/零角零分$/, '整').replace(/零[仟佰拾]/g, '零').replace(/零{2,}/g, '零').replace(/零([亿|万])/g, '$1').replace(/零+元/, '元').replace(/亿零{0,3}万/, '亿').replace(/^元/, "零元");
-
};
-
-
function cmycurd(num)
-
{
-
var str1 = '零壹贰叁肆伍陆柒捌玖'; //0-9所对应的汉字
-
var str2 = '万仟佰拾亿仟佰拾万仟佰拾元角分'; //数字位所对应的汉字
-
var str3; //从原num值中取出的值
-
var str4; //数字的字符串形式
-
var str5 = ''; //人民币大写金额形式
-
var i; //循环变量
-
var j; //num的值乘以100的字符串长度
-
var ch1; //数字的汉语读法
-
var ch2; //数字位的汉字读法
-
var nzero = 0; //用来计算连续的零值是几个
-
-
num = Math.abs(num).toFixed(2); //将num取绝对值并四舍五入取2位小数
-
str4 = (num * 100).toFixed(0).toString(); //将num乘100并转换成字符串形式
-
j = str4.length; //找出最高位
-
if (j > 15){return '溢出';}
-
str2 = str2.substr(15-j); //取出对应位数的str2的值。如:200.55,j为5所以str2=佰拾元角分
-
-
//循环取出每一位需要转换的值
-
for(i=0;i<j;i++){
-
str3 = str4.substr(i,1); //取出需转换的某一位的值
-
if (i != (j-3) && i != (j-7) && i != (j-11) && i != (j-15)){ //当所取位数不为元、万、亿、万亿上的数字时
-
if (str3 == '0'){
-
ch1 = '';
-
ch2 = '';
-
nzero = nzero + 1;
-
}
-
else{
-
if(str3 != '0' && nzero != 0){
-
ch1 = '零' + str1.substr(str3*1,1);
-
ch2 = str2.substr(i,1);
-
nzero = 0;
-
}
-
else{
-
ch1 = str1.substr(str3*1,1);
-
ch2 = str2.substr(i,1);
-
nzero = 0;
-
}
-
}
-
}
-
else{ //该位是万亿,亿,万,元位等关键位
-
if (str3 != '0' && nzero != 0){
-
ch1 = "零" + str1.substr(str3*1,1);
-
ch2 = str2.substr(i,1);
-
nzero = 0;
-
}
-
else{
-
if (str3 != '0' && nzero == 0){
-
ch1 = str1.substr(str3*1,1);
-
ch2 = str2.substr(i,1);
-
nzero = 0;
-
}
-
else{
-
if (str3 == '0' && nzero >= 3){
-
ch1 = '';
-
ch2 = '';
-
nzero = nzero + 1;
-
}
-
else{
-
if (j >= 11){
-
ch1 = '';
-
nzero = nzero + 1;
-
}
-
else{
-
ch1 = '';
-
ch2 = str2.substr(i,1);
-
nzero = nzero + 1;
-
}
-
}
-
}
-
}
-
}
-
if (i == (j-11) || i == (j-3)){ //如果该位是亿位或元位,则必须写上
-
ch2 = str2.substr(i,1);
-
}
-
str5 = str5 + ch1 + ch2;
-
-
if (i == j-1 && str3 == '0' ){ //最后一位(分)为0时,加上“整”
-
str5 = str5 + '整';
-
}
-
}
-
if (num == 0){
-
str5 = '零元整';
-
}
-
return str5;
-
}
2.
3.
阅读(1655) | 评论(0) | 转发(0) |