如果数字的位数不超过6位且小数位数不超过2位,我需要返回验证检查(布尔值).
例如:
1 = valid
10 = valid
111111 = valid
111111.11 = valid
1111111.11 = INVALID
1.111 = INVALID
Run Code Online (Sandbox Code Playgroud)
通过堆栈溢出来查看我只能找到输入自动舍入的答案(不是我想要的)或小数位必须等于2个小数位(最多2个).
小智 6
显然,你需要
function valid(n) {
return no_more_than_six_digits(n) && no_more_than_two_decimal_places(n);
}
Run Code Online (Sandbox Code Playgroud)
那么我们如何定义这些功能呢?
function no_more_than_six_digits (n) { return n < 1e7; }
function no_more_than_two_decimal_places(n) { return Math.floor(n * 100) === n * 100; }
Run Code Online (Sandbox Code Playgroud)