util.js 642 B

123456789101112131415161718192021
  1. export const numInputInput = (value, len = 5, decimals = 1) => {
  2. // value:修改的数字 len 总长度 decimals:允许小数点后几位
  3. //todo 5位整数数加一位小数
  4. value += "";
  5. if (value.length == 0) return null;
  6. let index = value.lastIndexOf(".");
  7. if (index != -1) {
  8. let [int, dec] = value.split(".");
  9. if (dec.length > decimals) {
  10. dec = dec.slice(0, dec.length - 1);
  11. }
  12. if (int.length > len) {
  13. int = int.slice(0, int.length - 1);
  14. }
  15. return parseFloat(int + "." + dec);
  16. }
  17. if (value.length > len) {
  18. value = value.slice(0, value.length - 1);
  19. }
  20. return parseFloat(value);
  21. }