code: public static Optional<? extends Number> toNumber(Object arg) {
          if ( arg instanceof Number ) {
              return Optional.of( ( (Number) arg ));
          }
          else if(arg instanceof String) {
              try {
                  return Optional.of( (Number) Integer.parseInt( (String) arg ) );
              }
              catch(Exception ignored) {}
              try {
                  return Optional.of( (Number) Long.parseLong( (String) arg ) );
              }
              catch(Exception ignored) {}
              try {
                  return Optional.of( (Number) Double.parseDouble( (String) arg ) );
              }
              catch(Exception ignored) {}
              return Optional.empty();
          }
          else {
              return Optional.empty();
          }
      }
docs: Given any object, returns, if possible. its Java number equivalent wrapped in Optional
  Interprets String as Number
  
  toNumber("123") == Optional.of(123)
  toNumber("-123") == Optional.of(-123)
  toNumber("12.3") == Optional.of(12.3)
  
  toNumber("abc") == Optional.empty()
  toNumber(null) == Optional.empty()
  
  also, see: MathTest#testNitPicks