Authored by lijing

强制保留两位小数

@@ -158,8 +158,9 @@ $( @@ -158,8 +158,9 @@ $(
158 // 插入倒计时 158 // 插入倒计时
159 addTimeout($('.price-date')); 159 addTimeout($('.price-date'));
160 addTimeout($('.text-info')); 160 addTimeout($('.text-info'));
161 - $('.current-price').text('¥' + data.secKillPrice).show();  
162 - $('.sale-price').text('¥' + data.secKillPrice).show(); 161 + let secKillPrice = toDecimal2(data.secKillPrice);
  162 + $('.current-price').text('¥' + secKillPrice).show();
  163 + $('.sale-price').text('¥' + secKillPrice).show();
163 $('.chose-items .num').find('.clearfix').append( 164 $('.chose-items .num').find('.clearfix').append(
164 '<span class="limit-num-text">限购1件</span>' 165 '<span class="limit-num-text">限购1件</span>'
165 ); 166 );
@@ -300,3 +301,22 @@ function addTimeout(obj) { @@ -300,3 +301,22 @@ function addTimeout(obj) {
300 '</div>' 301 '</div>'
301 ); 302 );
302 } 303 }
  304 +
  305 +// 强制保留2位小数点
  306 +function toDecimal2(num) {
  307 + var f = parseFloat(num);
  308 + if (isNaN(f)) {
  309 + return false;
  310 + }
  311 + var f = Math.round(num * 100) / 100;
  312 + var s = f.toString();
  313 + var rs = s.indexOf('.');
  314 + if (rs < 0) {
  315 + rs = s.length;
  316 + s += '.';
  317 + }
  318 + while (s.length <= rs + 2) {
  319 + s += '0';
  320 + }
  321 + return s;
  322 +}