Created
January 3, 2023 23:40
-
-
Save EnderGamingFilms/911e66cbfba94e5227f6e58041e1c129 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NumUtils { | |
public static Integer parseInt(String str, Integer def) { | |
try { | |
return Integer.parseInt(str); | |
} catch (NumberFormatException e) { | |
return def; | |
} | |
} | |
public static Double parseDouble(String str, Double def) { | |
try { | |
return Double.parseDouble(str); | |
} catch (NumberFormatException e) { | |
return def; | |
} | |
} | |
public static Float parseFloat(String str, Float def) { | |
try { | |
return Float.parseFloat(str); | |
} catch (NumberFormatException e) { | |
return def; | |
} | |
} | |
public static Long parseLong(String str, Long def) { | |
try { | |
return Long.parseLong(str); | |
} catch (NumberFormatException e) { | |
return def; | |
} | |
} | |
public static Short parseShort(String str, Short def) { | |
try { | |
return Short.parseShort(str); | |
} catch (NumberFormatException e) { | |
return def; | |
} | |
} | |
public static Byte parseByte(String str, Byte def) { | |
try { | |
return Byte.parseByte(str); | |
} catch (NumberFormatException e) { | |
return def; | |
} | |
} | |
public static Boolean parseBoolean(String str) { | |
return str.matches("true|1|yes"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment