因为客户的要求,要将一串数字转换成类似xxx万xxxx元的格式,所以就试着写了下面的代码:

CSS:


.fl{
	float:left;
}
#nums{
	    height: 20px;
    line-height: 20px;
    border: 1px solid #ccc;
    padding-left: 3px;
}
#btn_go{
	background: #ED3D7D;
    padding: 2px 10px;
    color: #fff;
    border: 0px;
}


HTML:


<input id="nums" onkeyup="value=this.value.replace(/\D+/g,'')"/>
<button id="btn_go">点击拆分</button>
<div class="history_money_all_big_box">
  <div class="history_money_all_box fr">
    <div class="fl money_man_box">
      <div id="man_nums" class="nums_anime fl">0</div>
      <div id="man_nums_chache" style="display:none;"></div>
      <div class="fl">万</div>
    </div>
    <div class="fl">
      <div id="qian_nums" class="nums_anime fl">0</div>
      <div id="qian_nums_chache" style="display:none;"></div>
    </div>
    <div class="fl">元</div>
    <!--总金额输入到这里(无需拆分,必须为整数)-->
    <div id="all_money_cache" style="display:none;">31087</div>
  </div>
</div>


JS:


<script>
$(document).ready(function () {
	$('#btn_go').click(function(){
	var nums = $('#nums').val();
	/*判断有没有输入数字*/
	if(nums==""){
		return false
	}
	/*先将数字除以10000小数点前的就是万元数字*/
	var man_qian_num = parseInt(nums)/10000; 
    var strs_qian=man_qian_num.toFixed(4); 
	/*小数点进行拆分*/
    var arr_qian = strs_qian.split(".");
	
   /*输出万位后面的数字*/
   $('#qian_nums').html(arr_qian[arr_qian.length - 1]);
   

   var strsb_man=man_qian_num.toFixed(0); 
   /*判断万位数是否为0*/
   if(strsb_man<=0){
	   /*如果万位数为0则要筛选数字前面的0*/
	   var qian_zero = $('#qian_nums').html();
	   $('#qian_nums').html(qian_zero.replace(/\b(0+)/gi,""))
   }
   /*如果万位数后面本身是0*/
   if(qian_zero<=0){
	   /*直接输出0*/
	   $('#qian_nums').html("0")
   }
   /*输出万位数前面的数字*/
   $('#man_nums').html(strsb_man)
   });
});
</script>


效果:

0
0