Search code examples
scalarequire

Checking the number of digits in Scala


I need to to check whether the number of digits in a number are equal to some other number. The best way I could come up with was this:

require(Number.toString  == """\d{8}""", throw new InvalidDateFormatException("Wrong format for string parameter"))

where the number of digits required are 8. Is there any better way?


Solution

  • One alternative:

    require(Number.toString.length == 8, throw new InvalidDateFormatException("Wrong format for string parameter"))