Browse Source

小数位判断

laiguoran 2 years ago
parent
commit
b691e4d532
3 changed files with 27 additions and 4 deletions
  1. 23 2
      app/public/js/payment_detail.js
  2. 1 1
      app/view/payment/detail.ejs
  3. 3 1
      config/web.js

+ 23 - 2
app/public/js/payment_detail.js

@@ -29,9 +29,23 @@ $(function () {
     getAllList();
 
     $('#rpt-form input').on('change', function () {
-       const newVal = $(this).val();
+       let newVal = $(this).val();
        const index = parseInt($(this).data('index'));
-        checkAndUpdate(index, newVal, $(this));
+       const type = $(this).attr('type');
+       if (type === 'number' && newVal) {
+           const val = _.toNumber(newVal);
+           if (countDigits(val) > 11) {
+               toastr.error('请输入11位整数内的数');
+               return;
+           }
+           const reg = new RegExp("^\\d+(\\.\\d{1,"+ 6 +"})?$");
+           if (!reg.test(val)) {
+               toastr.warning('已保留6位小数');
+               newVal = ZhCalc.round(val, 6);
+               $(this).val(newVal);
+           }
+       }
+       checkAndUpdate(index, newVal, $(this));
     });
 
     $('#rpt-form textarea').on('change', function () {
@@ -315,6 +329,13 @@ function getAllList(currPageNum = 1) {
         $(this).addClass('bg-light');
     });
 }
+// 整数位数判断
+function countDigits(number) {
+    var numberString = Math.abs(number).toString();
+    var decimalIndex = numberString.indexOf('.'); // 查找小数点的索引
+    var integerPart = decimalIndex === -1 ? numberString : numberString.slice(0, decimalIndex); // 如果不存在小数点,则整个数字都是整数部分
+    return integerPart.length;
+}
 
 function bytesToSize(bytes) {
     if (parseInt(bytes) === 0) return '0 B';

+ 1 - 1
app/view/payment/detail.ejs

@@ -65,7 +65,7 @@
                                             <% if (item.type === 'textarea') { %>
                                                 <textarea <% if (ctx.detail.readOnly) { %>readonly<% } %> class="form-control form-control-sm" data-index="<%- item.index %>"><%- item.value %></textarea>
                                             <% } else { %>
-                                                <input <% if (ctx.detail.readOnly) { %>readonly<% } %> type="<%- item.type %>" value="<%- item.type === 'number' && ctx.helper._.indexOf(item.value, '元') !== -1 ? parseFloat(item.value) : item.value %>"  data-index="<%- item.index %>" class="form-control form-control-sm" placeholder="请输入">
+                                                <input <% if (ctx.detail.readOnly) { %>readonly<% } %> type="<%- item.type %>" value="<%- item.type === 'number' && ctx.helper._.indexOf(item.value, '元') !== -1 ? parseFloat(item.value) : item.value %>" <% if (item.type === 'number') { %>step="0.000001"<% } %>  data-index="<%- item.index %>" class="form-control form-control-sm" placeholder="请输入">
                                             <% } %>
                                         </div>
                                         <% } %>

+ 3 - 1
config/web.js

@@ -1160,8 +1160,10 @@ const JsFiles = {
             },
             detail: {
                 files: ['/public/js/datepicker/datepicker.min.js', '/public/js/datepicker/datepicker.zh.js', '/public/js/moment/moment.min.js',
-                    '/public/js/axios/axios.min.js', '/public/js/js-xlsx/jszip.min.js', '/public/js/file-saver/FileSaver.min.js'],
+                    '/public/js/axios/axios.min.js', '/public/js/js-xlsx/jszip.min.js', '/public/js/file-saver/FileSaver.min.js',
+                    '/public/js/decimal.min.js'],
                 mergeFiles: [
+                    '/public/js/zh_calc.js',
                     '/public/js/zip_oss.js',
                     '/public/js/payment_detail.js',
                     '/public/js/payment_detail_audit.js',