English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Determine whether the user's input contains at least N decimal places.
1An exception is thrown when the user enters non-numeric values, and false is returned.
2. When the user enters a number, judge whether it contains at least N decimal places. If it does not contain, return false.
3. When the decimal places of the number entered by the user are greater than or equal to N, return true.
public class ddouble { public static void main(String args[]) { String str = " .1124 "; boolean sfdouble = doubleyn(str,4); System.out.println(sfdouble); } public static boolean doubleyn(String str,int dousize){ try { double num=Double.valueOf(str);//Force the string to be converted to a number if(str.trim().indexOf(".")=== -1{ return false; } int fourplace = str.trim().length() - str.trim().indexOf(".") - 1; if(fourplace<dousize){ return false; } return true; } } return false;//If an exception is thrown, return False } } }
The above example of the Java judgment of whether the user input contains at least N decimal places is all the content shared by the editor. I hope it can be a reference for everyone, and I also hope everyone will support the Yell Tutorial.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting. Provide relevant evidence, and once verified, this site will immediately delete the infringing content.)