In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "analyzing the transformation and output of Java BigDecimal toString ()". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "analyze the transformation and output of Java BigDecimal toString ()".
The toString () method of BigDecimal will output the BigDecimal as a string. This method will use exponents for output when necessary.
The specific conversion steps are as follows:
The absolute value of the non-scalar value of BigDecimal is not converted to a string base of 100 using the characters'0' to'9' (unless its value is zero, in which case a single'0' character is used).
Next, calculate the adjusted index; this is an invalid scale, plus the scale value of the character conversion, minus the number of times. That is,-scale+ (ulength-1), where ulength is the length of the absolute value of the non-standard value of the decimal number (its precision).
If the ratio is greater than or equal to zero and the adjusted index is greater than or equal to-6, the number is converted to the form of characters without the need for exponential representation.
In this case, if the ratio is zero, no decimal point is added and if the size of the positive decimal point will be inserted to the right of the specified number of characters. It is necessary to add the'0' character to the left side of the converted non-scalar value. If there is no character in front of it, the decimal point is inserted and then prefixed with the traditional'0' character.
Otherwise (that is, if the ratio is negative, or if the adjusted index is less than-6), the number will be converted to character form using an exponential representation. In this case, if the converted BigInteger has more than one decimal place after the first digit is inserted.
The exponent in character form, and then the suffix converts the non-scalar value (perhaps inserting the decimal point); this includes the exponent that is adjusted immediately after the letter "E" to convert to character form.
The latter has a cardinality of ten, uses the characters'0' to'9', has no leading zero, and is always followed by the prefix symbol character'-'('u002D'), otherwise it is' +'('u002B') if the adjusted exponent is negative.
Finally, the entire string is prefixed by a minus character ('u002D') if the non-scalar value is less than zero. If the non-scalar value is zero or a positive unsigned character is prefixed.
Convert BigDecimal to StringSRC without scientific counting
Please refer to the bigDecimalWithoutScientificNotationTest (): method in the source code on GitHub
Https://github.com/cwiki-us-demo/java-tutorials/blob/master/java-numbers/src/test/java/com/ossez/maths/BigDecimalDemoUnitTest.java
() {String input = RandomStringUtils. () + BigDecimal bigNumber = BigDecimal (input) String numberStr = bigNumber.toString () .info (numberStr)} OUTPUT
The final output of the program is as follows, which will output the converted string.
2020-02-25 09Viru 43perv 29.595 [main] INFO c.ossez.maths.BigDecimalDemoUnitTest-150327720097124567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678678901245678678124567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678
Convert BigDecimal to StringSRC using scientific counting
Please refer to the bigDecimalScientificNotation (): method in the source code on GitHub
Https://github.com/cwiki-us-demo/java-tutorials/blob/master/java-numbers/src/test/java/com/ossez/maths/BigDecimalDemoUnitTest.java
() {BigDecimal bigNumberString numberStrMathContext mc = MathContext () bigNumber = BigDecimal (RandomStringUtils. () + mc) .info (bigNumber.toString ()) .info (bigNumber.toEngineeringString ()) .info (bigNumber.toPlainString ())}
OUTPUT
The final output of the program is as follows, which will output the converted string.
2020-02-25 10VOUGOUR 50.909 [main] INFO c.ossez.maths.BigDecimalDemoUnitTest-8.31198E+14
2020-02-25 10VRV 00RV 50.912 [main] INFO c.ossez.maths.BigDecimalDemoUnitTest-831.198E+12
2020-02-25 1000VR 50.912 [main] INFO c.ossez.maths.BigDecimalDemoUnitTest-8311980000000
The BigDecimal class has three toString methods, which are toString (), toEngineeringString (), and toPlainString ().
You can see the difference between these three methods from the comments in BigDecimal:
ToString ()-use scientific counting if necessary
ToEngineeringString () uses the engineering counting method when necessary. Similar to the scientific counting method, except that the power of the index is a multiple of 3, which is convenient for engineering application, because it is 10 ^ 3 when many units are converted.
ToPlainString ()-does not use any scientific counting.
Do not use index
Scientific counting method
Engineering counting method
27002.7 × 10 / 2.7 × 10 / 270002.7 × 10 / 27 × 10 / 2700002.7 × 10 / 27 × 10 / 27000002.7 × 10 / 2 / 2 / 7 / 10
You can see that BigDecimal will output content in different formats depending on the output format requirements. This is convenient for us to convert and output in the later scientific calculation.
Thank you for your reading, the above is the content of "analyzing the conversion and output of Java BigDecimal toString ()". After the study of this article, I believe you have a deeper understanding of the problem of analyzing the conversion and output of Java BigDecimal toString (), and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.