On-Machine Topic (intermediate)-Output The frequency of the specified symbol in decimal form (Java)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.