vanilla JavaScript solution to check if number has no more than 6 digits and 2 decimal places -
i need return validation check (boolean) if number has no more 6 number of digits , no more 2 decimal places.
for example:
1 = valid 10 = valid 111111 = valid 111111.11 = valid 1111111.11 = invalid 1.111 = invalid
looking through stack overflow can find answers input automatically rounded (not want) or decimal places have equal 2 decimal places (not @ 2).
obviously, need
function valid(n) { return no_more_than_six_digits(n) && no_more_than_two_decimal_places(n); }
so how define these functions?
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; }
Comments
Post a Comment