给价格加.00

来源:互联网 发布:面试官面试技巧知乎 编辑:程序博客网 时间:2024/06/10 19:07

$result['pageData'][$k]['coursePrice'] = sprintf('%.2f', $v['coursePrice']);


$result['pageData'][$k]['discountPrice'] = sprintf('%.2f', $v['discountPrice']);



  1. //JS版  
  2.     //将传入数据转换为字符串,并清除字符串中非数字与.的字符  
  3.     //按数字格式补全字符串  
  4.     var getFloatStr = function(num){  
  5.         num += '';  
  6.         num = num.replace(/[^0-9|\.]/g, ''); //清除字符串中的非数字非.字符  
  7.           
  8.         if(/^0+/) //清除字符串开头的0  
  9.             num = num.replace(/^0+/, '');  
  10.         if(!/\./.test(num)) //为整数字符串在末尾添加.00  
  11.             num += '.00';  
  12.         if(/^\./.test(num)) //字符以.开头时,在开头添加0  
  13.             num = '0' + num;  
  14.         num += '00';        //在字符串末尾补零  
  15.         num = num.match(/\d+\.\d{2}/)[0];  
  16.     };  
  17.   
  18. //测试  
  19.   
  20.     getFloatStr('0000.1');  //0.10  
  21.     getFloatStr('qwe');       //0.00  
  22.     getFloatStr('256');       //256.00  
js版第二种方法
num=num.toFixed(2);

0 0
原创粉丝点击