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 ):
@Test
publicvoid testPrintScientificNotation(){
// When the number of digits in the integer part is greater than or equal to 8, it is displayed in scientific notation.
System.out.println(-12345678.0);
System.out.println(12345678.0);
// The integer is 0. When the decimal point starts to appear consecutively greater than or equal to 3, it is displayed in scientific notation.
System.out.println(0.0001);
System.out.println(-0.0001);
}
Result
-1.2345678E7
1.2345678E7
1.0E-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.
publicstaticString scientificNotation2String(Double d,int newValue){
String value =null;
NumberFormat nf =NumberFormat.getInstance();
// Set this format to not use groups
nf.setGroupingUsed(false);
// Set the maximum number of digits allowed for the decimal part.
nf.setMaximumFractionDigits(newValue);
value = nf.format(d);
return value;
}
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.
publicstaticString scientificNotation2String(Double d){
String value =null;
DecimalFormat decimalFormat = newDecimalFormat ("0.00"); // format settings
value = decimalFormat.format(d);
return value;
}
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.
publicstaticString scientificNotation2String(String str){
String value =null;
BigDecimal bd =newBigDecimal(str);
value = bd.toPlainString();
return value;
}
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.