The input input box is limited to only two valid decimals.

Getting started with the front-end is teaching. Today, the blogger shared a few small cases with practical front-end amounts. Copy it and use it!

   I believe that many front-end partners have encountered such a need in their work, that is, to limit the content of the input box to only enter two decimals. For students who want to use regular expressions but don’t know how to start, the blogger will share a small case , The next time you encounter this, don't panic at all.

    /**
         * @param row       //当前输入行
         * @param index     //当前输入字段
         * @param istype    //是否可以输入负号 例传'1'可以输入负号
                    */
            function limitedAmount(row,index,istype){
            const t = row[index].charAt(0);
            const x = row[index].charAt(1);
            if(t =='0' && x!='.' && row[index].length>1) row[index] = row[index].substr(1,row[index].length);
            row[index] =  row[index].replace(/[^\d.]/g, '');
            // 必须保证第一个为数字而不是.
            row[index] =  row[index].replace(/^\./g, '');
            // 保证只有出现一个.而没有多个.
            row[index] = row[index].replace(/\.{2,}/g, '.');
            // 保证.只出现一次,而不能出现两次以上
            row[index] =  row[index].replace('.', '$#$').replace(/\./g, '').replace('$#$', '.');
            // 保证 只允许输入两位小数
            row[index] =  row[index].replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
            // 如果你想保留4位小数把上面两位小数注释掉 用下面这句就行了
            // row[index] =  row[index].replace(/^(\-)*(\d+)\.(\d\d\d\d).*$/,'$1$2.$3');
            if(istype !='1') return;
            // 如果第一位是负号,则允许添加
            if (t == '-') {
                row[index] = '-' +  row[index]
            }
        }

    If you can only enter 0 and positive integers, you can use the following code to achieve:

    /**
 * 只能输入0和正整数
 * @param row    // 当前行
 * @param index  // 当前字段
 * @param type  // 是否可以输入 0 默认否
 */
function checkOnlyInputInteger(row,index,type){
  const t = row[index].charAt(0);
  if(t =='0' && !type) row[index] = row[index].substr(1,row[index].length);
  row[index] =  row[index].replace(/[^0-9]/g,'');
}

   The above is the restriction on the input of the input input box. Next, we will deal with the retention and display format of the amount. The amount in the form is reserved to two decimal places. If it is a percentage, the processing of the percent sign is displayed:

/**
 * 表格金额保留两位小数
 * @param val     // 当前值
 * @param isRata  // 是否百分号展示
 */
 function setAmountFormat(val,isRata){
  if(isRata =='1') return  parseFloat(val*100).toFixed(2) +'%';
  if(val) return  parseFloat(val).toFixed(2)||'';
}

    If a small partner needs to display the amount as a thousandths, you can use the following code to achieve it:

  function thousand(num) {
            <!-- 千分符正则表达式 -->
            return (num + "").replace(/\d(?=(\d{3})+$)/g, "$&,")
        }
  console.log(thousand(123456789))

    Amount minus thousandths:

function rmoney(e) {
        if (e) {
            return parseFloat(e.replace(/[^\d\.-]/g, ''));
        } else {
            return 0;
        }
 }

​​​​​​​​Well     , here the blogger mainly talked about some commonly used methods of handling money at work . If you have other needs, friends are welcome

     Talk to bloggers and discuss, happy time is always short, this is the end of our sharing in this issue, friends make a fortune

     This is not only support for bloggers but also a desire for knowledge! Shanshui meets again, see you next time! ! !

Guess you like

Origin blog.csdn.net/jw_xiaoming/article/details/127972752