/**
* JS实现数字转换为货币汉字大写数字的方法详解
* @param num
* @returns {string}
* @constructor
*/
function Chinese(num){
if(!/^\d*(\.\d*)?$/.test(num))throw(new Error(-1, "Number is wrong!"));
var AA = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
var BB = new Array("","拾","佰","仟","萬","億","圆","");
var CC = new Array("角", "分", "厘");
var a = (""+ num).replace(/(^0*)/g, "").split("."), k = 0, re = "";
for(var i=a[0].length-1; i>=0; i--){
switch(k){
case 0 : re = BB[7] + re; break;
case 4 : if(!new RegExp("0{4}\\d{"+ (a[0].length-i-1) +"}$").test(a[0]))
re = BB[4] + re; break;
case 8 : re = BB[5] + re; BB[7] = BB[5]; k = 0; break;
}
if(k%4 == 2 && a[0].charAt(i)=="0" && a[0].charAt(i+2) != "0") re = AA[0] + re;
if(a[0].charAt(i) != 0) re = AA[a[0].charAt(i)] + BB[k%4] + re; k++;
}
if(a.length>1){
re += BB[6];
for(var i=0; i<a[1].length; i++){
re += AA[a[1].charAt(i)] + CC[i];
if(i==2) break;
}
if(a[1].charAt(0)=="0" && a[1].charAt(1)=="0"){
re+="元整";
}
}else{
re+="元整";
}
return re;
}
输出结果:
Chinese(20)——"贰拾元整"
/
// 说明:Javascript 获取链接(url)参数的方法
// 整理:http://www.CodeBit.cn
function getQueryString(name){
// 如果链接没有参数,或者链接中不存在我们要获取的参数,直接返回空
if(location.href.indexOf("?")==-1 || location.href.indexOf(name+'=')==-1){
return "";
}
// 获取链接中参数部分
var queryString = location.href.substring(location.href.indexOf("?")+1);
// 分离参数对 ?key=value&key2=value2
var parameters = queryString.split("&");
var pos, paraName, paraValue;
for(var i=0; i<parameters.length; i++){
pos = parameters[i].indexOf('=');
if(pos == -1) { continue; }
paraName = parameters[i].substring(0, pos);
paraValue = parameters[i].substring(pos + 1);
// 如果查询的name等于当前name,就返回当前值,同时,将链接中的+号还原成空格
if(paraName == name){
return unescape(paraValue.replace(/\+/g, " "));
}
}
return "";
};
/**
* 设置url参数
* @param parmes
* @returns {string}
* example:setQueryString(location.href,new Array({key:"flag",value:"H123"},{key:"martix",value:"1020101201020100"}))
*/
function setQueryString(url,parmes){
if(url.indexOf("?") != -1){
url = url.split("?")[0];
}
url = url + "?";
for( var i = 0 ;i < parmes.length;i++){
var key=parmes[i].key;
var value=parmes[i].value;
url = url+"&"+key+"="+value;
}
return url;
};
/**
*判断两矩形是否相交,若相交则返回相交后的矩形
*
*/
function getRectangle(rect01MinPoint,rect01MaxPoint,rect02MinPoint,rect02MaxPoint) {
var rect=null;
var minPx ,minPy, maxPx,maxPy;
minPx=Math.max(rect01MinPoint.x,rect02MinPoint.x);
minPy=Math.max(rect01MinPoint.y,rect02MinPoint.y);
maxPx=Math.min(rect01MaxPoint.x,rect02MaxPoint.x);
maxPy=Math.min(rect01MaxPoint.y,rect02MaxPoint.y);
if(minPx<maxPx&& minPy<maxPy){
rect=new Rectangle(cc.p(minPx,minPy),cc.p(maxPx,maxPy));
}
return rect;
}
/**
* 矩形
* 在调用之前需要注意 centerPoint指两矩形的中心点坐标。
**/
function Rectangle(minPoint, maxPoint){
Rectangle.prototype.minPoint= minPoint;
Rectangle.prototype.maxPoint= maxPoint;
}
/**
* 二维转一维
*/
function twoDegreeToOneDegree(twoDegree){
var oneDegree=[];
for(var i=0;i<twoDegree.length;i++){
var arrs = twoDegree[i];
for(var j=0;j<arrs.length;j++){
oneDegree.push(arrs[j]);
}
}
return oneDegree;
}
/**
* 把一位数组右转90度,后在返回旋转后的一维数组
* @param oneDegree
* @param row
* @param col
* @returns {*}
*/
function twoDegreeConvertRight90(oneDegree,row,col){
var convert90Degree = new Array();
for(var i=0;i<row;i++){
var arr=new Array();
for(var j=0;j<col;j++){
arr[j] = -1
}
convert90Degree[i]=arr;
}
var k=0;
for(var i=col-1;i>=0;i--){
for(var j=0;j<row;j++){
convert90Degree[j][i]=oneDegree[k];
k++;
}
}
return twoDegreeToOneDegree(convert90Degree);
}
/**
* 替换所有的回车换行
* @param content
* @returns {*}
* @constructor
*/
function TransferString(content)
{
var string = content;
try{
string=string.replace(/(\n)+|(\r\n)+/g, "") //换行
//string=string.replace(/^\s*(\S+)\s*$/, "") //空格
}catch(e) {
alert(e.message);
}
return string;
}
js常用方法
最新推荐文章于 2024-04-14 07:00:00 发布