The topics are as follows:
The code is as follows:
Package Huawei;import Java.math.bigdecimal;public Final class Demo {/* * Feature: The frequency string that counts the occurrences of a specified character in the input reference string includes only English uppercase and lowercase letters and spaces, commas, dot numbers 2 valid digits after the decimal point, third rounding * * Input: String pstring input datum string char c specified character * * return: Output The frequency of the specified character */public static float Getrat Efromstring (String pstring, char c) {int num = 0;char pchar[] = Pstring.tochararray (); for (int i = 0; i < pchar.length; i++) {if (pchar[i] = = c) {num++;}} BigDecimal B = new BigDecimal ((float) num/pchar.length), float f1 = (float) b.setscale (2, bigdecimal.round_half_up). Doubl Evalue ();//reserved Two decimal places return F1;}}
Note that the division must be cast to float first, otherwise it may lose precision, and it is not correct. In addition, there are many ways to keep two decimal places.
On-Machine Topic (intermediate)-Output The frequency of the specified symbol in decimal form (Java)