money格式转换的实现
规则描述
format与value
- 以【#、0、小数点、逗号、负号】描述;如#,###0.00
- #的数量不影响显示效果,但效果一致;如#######与###效果一致
- 逗号的数量不影响显示效果,位置不限制,但最后数字显示都是千分位划分;如12,346,589
- 0的数量影响显示效果,整数部分一个0,表示数值不存在时,显示0.整数部分两个0,表示当数值小于10时显示0加数值。小数部分的0的数量表示小数显示多少位,超出部分以四舍五入的方式,不足的以0补位。例如:format="###,###00.00" value=“1.576”,实现结果是01.58。
- format支持负号
- format支持前后添加内容,如:format="$###,###00.00元"
- value支持数字与字符串类型
- value支持数值前后添加内容,如:value="$123456/人"
- value与format都可以为空,value默认值为0,format默认值为######0或######0.00
Javascript部分
function moneyFormat(value, format, defaultValue) {
var tempVal = value + '';
// 判断value是否存在
var isExists_value_return = isExists_value(tempVal, defaultValue);
// 判断format是否存在
var isExists_format_return = isExists_format(tempVal, format);
var newValue = isExists_value_return + "";
//确定值为正还是负;
if(newValue.indexOf("-") != -1){
var negative = "-";
newValue = newValue.split("-")[1];
}else{
var negative = '';
}
//判断值是否为纯数字
var valueFirstWord = "";
var valueLastWord = "";
if(!checkNumber(isExists_value_return)){
var tempArr = newValue.match(/\d+(\.\d+)?/g);
if(tempArr == null){
return "";
}
valueFirstWord = newValue.split(tempArr[0])[0];
valueLastWord = newValue.split(tempArr[0])[newValue.split(tempArr).length - 1];
newValue = tempArr[0];
}
// 解析format
var parseFormat_return = parseFormat(isExists_format_return);
if(parseFormat_return){
// 小数与整数分别处理
var firstValue = newValue.split(".")[0];
var lastValue = '';
if(parseFormat_return['isDecima']){
lastValue = newValue.split(".")[1];
}else{
newValue = Math.round(parseInt(newValue)) + '';
}
// 对小数与整数根据format进行补0处理
if(newValue.indexOf(".") != -1){
if(parseFormat_return['lastDecima_zeroLen'] < lastValue.length){
var temp = parseInt("1" + joinZero("0",parseFormat_return['lastDecima_zeroLen']));
var tempVal = Math.round(temp * parseFloat(newValue)) / temp;
newValue = tempVal;
}else if(parseFormat_return['lastDecima_zeroLen'] > lastValue.length){
var temp = joinZero("0",parseFormat_return['lastDecima_zeroLen'] - lastValue.length);
newValue = newValue + temp;
}else{
}
lastValue = "." + newValue.toString().split(".")[1];
}else{
if(parseFormat_return['firstDecima_zeroLen'] > firstValue.length){
var temp = joinZero("0",parseFormat_return['firstDecima_zeroLen'] - firstValue.length);
newValue = temp + newValue;
}
firstValue = newValue.toString().split(".")[0];
}
// 对整数部分进行逗号分隔
if(parseFormat_return['isComma']){
var j = firstValue.length > 3 ? firstValue.length % 3 : 0;
return parseFormat_return['firstword'] + valueFirstWord + negative + (j ? firstValue.substr(0, j) + ',' : '') + firstValue.substr(j).replace(/(\d{3})(?=\d)/g, ("$1" + ',')) + lastValue + valueLastWord + parseFormat_return['endword'];
}else{
return parseFormat_return['firstword'] + valueFirstWord + negative + firstValue + lastValue + valueLastWord + parseFormat_return['endword'];
}
}else{
return;
}
}
function joinZero(str, num){
return num > 1 ? str += joinZero(str, --num): str;
}
// 判断value是否存在
function isExists_value(value, defaultValue) {
var newValue = value.trim();
if (newValue == '' || newValue == null || newValue == undefined) {
if (defaultValue) {
if(checkNumber(defaultValue)){
newValue = defaultValue;
}else{
newValue = "0";
}
} else {
newValue = "0";
}
} {
}
return newValue;
}
// 判断format是否存在
function isExists_format(value, format) {
if (value.indexOf(".") != -1) {
var defaultFormat = "######0.00";
} else {
var defaultFormat = "######0";
}
if (format == '' || format == null || format == undefined) {
format = defaultFormat;
} else {
}
return format;
}
// 解析format
function parseFormat(format) {
var resultJson = {};
var firstword = "";
var endword = "";
var firstDecima_zeroLen = 0;
var lastDecima_zeroLen = 0;
if (format.indexOf(',') != -1) {
var isComma = true;
} else {
var isComma = false;
}
resultJson['isComma'] = isComma;
var wordArr = format.split(/\#|0|\,|\./g);
wordArr[0] ? firstword = wordArr[0] : "";
wordArr[wordArr.length - 1] ? endword = wordArr[wordArr.length - 1] : "";
resultJson['firstword'] = firstword;
resultJson['endword'] = endword;
if (format.indexOf('.') != -1) {
var isDecima = true;
var firstDecima = format.split(".")[0];
var lastDecima = format.split(".")[1];
firstDecima_zeroLen = zeroLength(firstDecima);
lastDecima_zeroLen = zeroLength(lastDecima);
} else {
firstDecima_zeroLen = zeroLength(format);
var isDecima = false;
}
resultJson['isDecima'] = isDecima;
resultJson['firstDecima_zeroLen'] = firstDecima_zeroLen;
resultJson['lastDecima_zeroLen'] = lastDecima_zeroLen;
return resultJson;
}
// 确定format中0的数量
function zeroLength(value){
var len = 0;
if(value.indexOf("0") != -1){
for(let i in value){
if(value[i] == "0"){
if(len != 0){
if(value[i-1] != "0"){
len = 0;
}
}
len += 1;
}else{
continue;
}
}
}
return len;
}
// 判断值是否是数字
function checkNumber(value) {
var reg = /^[0-9]+\.?[0-9]*$/;
if (reg.test(value)) {
return true;
} else {
return false;
}
}