Java Franction Convert fractionOfStringUppercase(String input)

Here you can find the source of fractionOfStringUppercase(String input)

Description

Of the characters in the string that have an uppercase form, how many are uppercased?

License

Open Source License

Parameter

Parameter Description
input Input string.

Return

The fraction of uppercased characters, with 0.0d meaning that all uppercasable characters are in lowercase and 1.0d that all of them are in uppercase.

Declaration

public static double fractionOfStringUppercase(String input) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from w  ww  .j a  va  2s  . co  m*/
     * Of the characters in the string that have an uppercase form, how many are uppercased?
     *
     * @param input Input string.
     * @return The fraction of uppercased characters, with {@code 0.0d} meaning that all uppercasable characters are in
     * lowercase and {@code 1.0d} that all of them are in uppercase.
     */
    public static double fractionOfStringUppercase(String input) {
        if (input == null) {
            return 0;
        }

        double upperCasableCharacters = 0;
        double upperCount = 0;
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            char uc = Character.toUpperCase(c);
            char lc = Character.toLowerCase(c);
            // If both the upper and lowercase version of a character are the same, then the character has
            // no distinct uppercase form (e.g., a digit or punctuation). Ignore these.
            if (c == uc && c == lc) {
                continue;
            }

            upperCasableCharacters++;
            if (c == uc) {
                upperCount++;
            }
        }

        return upperCasableCharacters == 0 ? 0 : upperCount / upperCasableCharacters;
    }
}

Related

  1. fraction(StringBuilder buf, int scale, long ms)
  2. fractionAlongRay(double px, double py, double qx, double qy, double rx, double ry)
  3. fractionalPart(float fnum)
  4. fractionalTime(int hour, int minute, int second)
  5. fractionDigits(double number)
  6. fractionToDecimal(String fraction)