当前位置:网站首页>保留一位小数和保留两位小数
保留一位小数和保留两位小数
2022-06-24 06:46:00 【张小浪】
保留一位小数
export const inputTake2AfterDot =(obj:any)=>{
// return value.toString().match(/^\d+(?:\.\d{0,2})?/)
// 清除"数字"和"."以外的字符
obj.value = obj.value.replace(/[^\d.]/g,"");
// 验证第一个字符是数字
obj.value = obj.value.replace(/^\./g,"");
// 只保留第一个, 清除多余的
obj.value = obj.value.replace(/\.{2,}/g,".");
obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
// 只能输入一个小数
obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d).*$/,'$1$2.$3');
// return Math.round(value*Math.pow(10,2))/Math.pow(10,2)
};保留两位小数
export const inputTake2AfterDot =(obj:any)=>{
// return value.toString().match(/^\d+(?:\.\d{0,2})?/)
// 清除"数字"和"."以外的字符
obj.value = obj.value.replace(/[^\d.]/g,"");
// 验证第一个字符是数字
obj.value = obj.value.replace(/^\./g,"");
// 只保留第一个, 清除多余的
obj.value = obj.value.replace(/\.{2,}/g,".");
obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
// 只能输入两个小数
obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
// return Math.round(value*Math.pow(10,2))/Math.pow(10,2)
};区别就在最后

这里我是在 antd 中的Input调用的 如下:
<Input placeholder={'工时'} value={myTaskStore.workingHours} onChange={this.hoursChange} suffix="小时"
onInput={(e:any)=>{
let obj = e.target;
inputTake2AfterDot(obj)
}}
/>
边栏推荐
- Combine with (& &) logic or (||), dynamic binding and ternary operation
- 图形技术之坐标转换
- How to realize multi protocol video capture and output in video surveillance system?
- 线程的阻塞问题
- 阿里云全链路数据治理
- 随机数备注
- Phonics
- Reppoints: Microsoft skillfully uses deformation convolution to generate point sets for target detection, full of creativity | iccv 2019
- Deploy L2TP in VPN (Part 2)
- [frame rate doubling] development and implementation of FPGA based video frame rate doubling system Verilog
猜你喜欢
![[equalizer] bit error rate performance comparison simulation of LS equalizer, def equalizer and LMMSE equalizer](/img/45/61258aa20cd287047c028f220b7f7a.png)
[equalizer] bit error rate performance comparison simulation of LS equalizer, def equalizer and LMMSE equalizer
![buuctf misc [UTCTF2020]docx](/img/e4/e160f704d6aa754e85056840e14bd2.png)
buuctf misc [UTCTF2020]docx

When MFC uses the console, the project path cannot have spaces or Chinese, otherwise an error will be reported. Lnk1342 fails to save the backup copy of the binary file to be edited, etc

Analog display of the module taking software verifies the correctness of the module taking data, and reversely converts the bin file of the lattice array to display

相机标定(标定目的、原理)
![(cve-2020-11978) command injection vulnerability recurrence in airflow DAG [vulhub range]](/img/33/d601a6f92b1b73798dceb027263223.png)
(cve-2020-11978) command injection vulnerability recurrence in airflow DAG [vulhub range]

阿里云全链路数据治理

Win11 points how to divide disks? How to divide disks in win11 system?

RDD basic knowledge points
![[OGeek2019]babyrop](/img/74/5f93dcee9ea5a562a7fba5c17aab76.png)
[OGeek2019]babyrop
随机推荐
【008】表格数据逐行筛选,跳出for循环及跳过本次循环思路_#VBA
Lend you a pair of insight, Frida native trace
How to delete / select an input method on your computer
Super fast reading in OI
Global and Chinese market of offshore furnaces 2022-2028: Research Report on technology, participants, trends, market size and share
《canvas》之第4章 线条操作
jarvisoj_ level2
向量操作与坐标转换相关方法
Actual target shooting - skillfully use SMB to take down the off-line host
In the era of industrial Internet, there are no more centers in the real sense, and these centers just turn tangible into intangible
PNAs: Geometric renormalization reveals the self similarity of multi-scale human connectome
BOM笔记
How to turn on win11 notebook power saving mode? How to open win11 computer power saving mode
Deploy L2TP in VPN (Part 1)
图形技术之管线概念
Global and Chinese market of digital fryer 2022-2028: Research Report on technology, participants, trends, market size and share
New features of PHP: bytecode cache and built-in server
How to connect the Bluetooth headset to the computer and how to connect the win10 computer to the Bluetooth headset
What industries and scenarios can the easynvr/easygbs/easycvr platform developed by tsingsee green rhino video be used in?
What should I pay attention to after the live broadcast system source code is set up?