当前位置:网站首页>Keep one decimal place and two decimal places

Keep one decimal place and two decimal places

2022-06-24 07:52:00 Zhangxiaolang

Keep one decimal place

export const inputTake2AfterDot =(obj:any)=>{
  // return value.toString().match(/^\d+(?:\.\d{0,2})?/)
  //  eliminate " Numbers " and "." Characters other than 
  obj.value = obj.value.replace(/[^\d.]/g,"");
  //  Verify that the first character is a number 
  obj.value = obj.value.replace(/^\./g,"");
  //  Keep only the first ,  Remove excess 
  obj.value = obj.value.replace(/\.{2,}/g,".");
  obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
  //  Only one decimal can be entered 
  obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d).*$/,'$1$2.$3');
  // return Math.round(value*Math.pow(10,2))/Math.pow(10,2)
};

Keep two decimal places

export const inputTake2AfterDot =(obj:any)=>{
  // return value.toString().match(/^\d+(?:\.\d{0,2})?/)
  //  eliminate " Numbers " and "." Characters other than 
  obj.value = obj.value.replace(/[^\d.]/g,"");
  //  Verify that the first character is a number 
  obj.value = obj.value.replace(/^\./g,"");
  //  Keep only the first ,  Remove excess 
  obj.value = obj.value.replace(/\.{2,}/g,".");
  obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
  //  Only two decimals can be entered 
  obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
  // return Math.round(value*Math.pow(10,2))/Math.pow(10,2)
};

The difference is at the end

Here I am antd Medium Input Called as follows :

<Input placeholder={' Working hours '} value={myTaskStore.workingHours} onChange={this.hoursChange} suffix=" Hours "
                       onInput={(e:any)=>{
                         let obj = e.target;
                         inputTake2AfterDot(obj)
                       }}
                />

 

原网站

版权声明
本文为[Zhangxiaolang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240328341253.html