满意答案
//JS版
//将传入数据转换为字符串,并清除字符串中非数字与.的字符
//按数字格式补全字符串
var getFloatStr = function(num){
num += '';
num = num.replace(/[^0-9|\.]/g, ''); //清除字符串中的非数字非.字符
if(/^0+/) //清除字符串开头的0
num = num.replace(/^0+/, '');
if(!/\./.test(num)) //为整数字符串在末尾添加.00
num += '.00';
if(/^\./.test(num)) //字符以.开头时,在开头添加0
num = '0' + num;
num += '00'; //在字符串末尾补零
num = num.match(/\d+\.\d{2}/)[0];
};
//测试
getFloatStr('0000.1'); //0.10
getFloatStr('qwe'); //0.00
getFloatStr('256'); //256.00
00分享举报