Java scientific notation and java notation

Source: Internet
Author: User

Java scientific notation and java notation
Java scientific notation
1. Concepts of scientific notation
1.1 valid numbers
1.2 E mark
2 scientific notation in Java
2.1 NumberFormat
DecimalFormat 2.2
2.3 BigDecimal
Reference
Author Information 1 concepts of scientific notation 1.1 valid numbers

In an approximate number, from the first number on the left to the number of digits accurate to the number of digits, all the digits in the middle are called the valid number of the approximate number.
For example:
890314000 retain three valid numbers to the power of 8.90x10 (four homes)
839960000 retain three valid numbers to the power of 8.40x10 (five in)
0.00934593 retain three valid numbers to the power of-3 of 9.35x10

1.2 E mark

Most calculators and computer programs use scientific notation to display very large and very small results. Because it is inconvenient to display the index (for example, 1011) on the screen, E or E is usually used to represent the ten power (writing "× 10b "), the number after E or e is its index. In other words, any two real numbers a and B (B should be an integer), and the value represented by "aEb" is a × 10b. Note that in this usage, the letter e is not a mathematical constant e, nor is it an exponential function exp () (using an uppercase letter E can avoid misunderstanding to a greater extent); although it also represents an index, but this symbol is usually called (Scientific Notation) E or e, rather than the base symbol in the index (although the latter will also appear ). Do not use this display method in official publications.
Note that e or E in scientific notation is irrelevant to the mathematical constant e or Function exp.
This is because it is inconvenient for some computer programs to write superscripts, and should not be used in official publications.
Scientific notation in Chinese national standards are expressed in the form of a × 10b, rather than aEb (see GB3101-1993, GBT15835-2011, GBT8170-2008 ).

2 scientific notation in Java

In Java, when the value of Double meets a certain condition, it is displayed in scientific notation (below is the result of a personal test, not the conclusion obtained from the document ):

  1. @Test
  2. publicvoid testPrintScientificNotation(){
  3. // When the number of digits in the integer part is greater than or equal to 8, it is displayed in scientific notation.
  4. System.out.println(-12345678.0);
  5. System.out.println(12345678.0);
  6. // The integer is 0. When the decimal point starts to appear consecutively greater than or equal to 3, it is displayed in scientific notation.
  7. System.out.println(0.0001);
  8. System.out.println(-0.0001);
  9. }

Result

  1. -1.2345678E7
  2. 1.2345678E7
  3. 1.0E-4
  4. -1.0E-4

Most of the time, we need to make a unified statement, either output in scientific notation or display all as normal counts.
According to the information on the Internet, the following three API implementation methods are mentioned: NumberFormat, DecimalFormat, and BigDecimal.

2.1 NumberFormat

NumberFormat is an abstract base class for all numeric formats.

  1. publicstaticString scientificNotation2String(Double d,int newValue){
  2. String value =null;
  3. NumberFormat nf =NumberFormat.getInstance();
  4. // Set this format to not use groups
  5. nf.setGroupingUsed(false);
  6. // Set the maximum number of digits allowed for the decimal part.
  7. nf.setMaximumFractionDigits(newValue);
  8. value = nf.format(d);
  9. return value;
  10. }

If the number of decimal places entered is greater than the set maximum number of decimal places, rounding is performed.

DecimalFormat 2.2

DecimalFormat is a subclass of NumberFormat used to format decimal numbers. This type of design has a variety of functions that enable it to parse and format numbers in any language environment, including support for Western, Arabic, and Hindi numbers. It also supports different types of numbers, including integers (123), fixed points (123.4), Scientific notation (1.23E4), percentages (12%), and amounts ($123 ). All these contents can be localized.

  1. publicstaticString scientificNotation2String(Double d){
  2. String value =null;
  3. DecimalFormat decimalFormat = newDecimalFormat ("0.00"); // format settings
  4. value = decimalFormat.format(d);
  5. return value;
  6. }

You need to set a template to specify the number of digits to be retained in decimal places. If the number of decimal places passed in exceeds the specified number of digits, It is rounded off. If the number of decimal places passed in is less than the specified number of digits, you can set zeros.
To convert a value to a scientific notation, you only need to modify the template. For example, you can change the template to 0. # E0.

2.3 BigDecimal

BigDecimal is an immutable, arbitrary-precision, signed decimal number.

  1. publicstaticString scientificNotation2String(String str){
  2. String value =null;
  3. BigDecimal bd =newBigDecimal(str);
  4. value = bd.toPlainString();
  5. return value;
  6. }

There are many construction methods for BigDecimal, not necessarily String-type values.
The difference between the toString method and the toPlanString method in BigDecimal:

  • ToString (): returns the string representation of the BigDecimal type. If an index is required, scientific notation is used.
  • ToPlainString (): returns the BigDecimal character transfer representation without an exponential field.
Reference
  • Interactive Encyclopedia: Scientific Notation: http://www.baike.com/wiki/%E7%A7%91%E5%AD%A6%E8% AE %A1%E6%95%B0%E6%B3%95
  • Blog: Java setting large numbers non-scientific notation display: http://blog.csdn.net/yangfeisc/article/details/42639075
  • Blog: how to make the double type in java not represented in scientific Notation: http://vincentboy.iteye.com/blog/1647187
  • Jdk api 1.6.0 Chinese Version
Author Information

Xiaozan Software Engineer
Blog: http://www.cnblogs.com/zhanyao/
Email: xzhanyao@foxmail.com

Welcome to raise your point.

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.