In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you the sample analysis of fractions to decimals in java, which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me take you to study and learn the "sample analysis of fractions to decimals in java".
Given two integers, represent the numerator numerator and denominator denominator of the fraction, respectively, and return the decimal as a string.
If the decimal part is a recurring decimal, enclose the loop in parentheses.
Example 1:
Enter: numerator = 1, denominator = 2
Output: "0.5"
Example 2:
Enter: numerator = 2, denominator = 1
Output: "2"
Example 3:
Enter: numerator = 2, denominator = 3
Output: "0. (6)"
Answer:
1public String fractionToDecimal (int numerator, int denominator) {
2 if (numerator = = 0) {
3 return "0"
4}
5 StringBuilder res = new StringBuilder ()
6 res.append ((numerator > 0) ^ (denominator > 0))? "-": ")
7 long num = Math.abs ((long) numerator)
8 long den = Math.abs ((long) denominator)
9 / / Integer part
10 res.append (num / den)
11 num% = den
12 if (num = = 0) {
13 return res.toString ()
14}
15 / / decimal part
16 res.append (.)
17 HashMap map = new HashMap ()
18 map.put (num, res.length ())
19 while (num! = 0) {
20 num * = 10
21 res.append (num / den)
22 num% = den
23 if (map.containsKey (num)) {
24 int index = map.get (num)
25 res.insert (index, ")
26 res.append (")")
27 break
28} else {
29 map.put (num, res.length ())
30}
31}
32 return res.toString ()
33}
Parsing:
Line 6 is the judgment symbol bit. If the divisor and divisor sign are the same, the result is positive, otherwise it is negative, the integer part is calculated first, and then the decimal part is calculated. The integer part is relatively simple, the decimal part we only need to save the remainder of each division, and then save it in HashMap, if there is a repeated description of the cycle, we can also refer to the previous 23, the countdown step.
What are the advantages of Java. Simple, as long as understand the basic concepts, you can write applications suitable for a variety of situations; 2. Object oriented; 3. Distributed, Java is a network-oriented language; 4. Robust, java provides automatic garbage collection for memory management to prevent programmers from making mistakes when managing memory. ; 5. Security, Java used in the network and distributed environment must prevent the invasion of viruses. 6. Architecturally neutral, as long as the Java runtime system is installed, it can run on any processor. 7. Portability, Java can be easily ported to different machines on the network. 8. Interpretation execution, Java interpreter directly interprets and executes Java bytecode.
The above is about "sample analysis of fractions to decimals in java". If this article is helpful to you and you think it is well written, please share it with your friends to learn new knowledge. if you want to know more about it, please pay more attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.