我如何将爪哇的两个小数点四舍五入



`公共类CheckVowelsDigits {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a String: ");
    String string = sc.next();
    int numOfVowels = 0;
    int numOfDigits = 0;
    string = string.toLowerCase();
    for (int i=0;i<string.length();i++){
       char letter = string.charAt(i);
       switch(letter){
           case 'a': 
           case 'e':
           case 'i':
           case 'o':
           case 'u': numOfVowels++; break;
       }
       if (letter >='0' && letter <='9')
          numOfDigits++;
    }
    double perVowels = (numOfVowels*1.0/string.length())*100.0;
    double perDigits = (numOfDigits*1.0/string.length())*100.0;
    System.out.println("Number of vowels: " + numOfVowels + " (" + perVowels + "%)");
    System.out.println("Number of digits: " + numOfDigits + " (" + perDigits + "%)");
}
}`

输出

输入字符串:test12345

元音数:1(11.11111111111111%(

数字数:5(55.5555555555556%(

但我需要

元音数:1(11.11%(

数字数:5(55.56%(

double pervowels = Math.Round(Pervowels * 100.0(/100.0;

最新更新