Printing of large double values in JAVA with or without scientific notation



Option 1: 
Double d= 0.0007 ;
System.out.println(d );

System.out.println(double d)
Prints a long and then terminate the line. This method behaves as though it invokes print(long) and then println().

print(double d)
 Prints a double-precision floating-point number. The string produced by String.valueOf(double) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

So System.out.println(double d) changes double value to String.
So 0.0007 becomes 7.0E-4.

Option 2:
Double d= 0.000000007;
System.out.println(BigDecimal.valueOf(d) );

If you use System.out.println( BigDecimal.valueOf(d)), Translates a double into a BigDecimal, using the double's canonical string representation provided by the Double.toString(double) method.

 So 0.0007 is printed as 0.00070 but if the double number value is large like,
 d= 0.000000007 then its becomes 7.0E-9 again.

Option 3: 
Double d= 0.000000007;
DecimalFormat df = new DecimalFormat("#.##########");
d = Double.parseDouble(df.format(d));
System.out.println( BigDecimal.valueOf(d) );

public StringBuffer format(double number,
                           StringBuffer result,
                           FieldPosition fieldPosition)
Formats a double to produce a string.

So first double is change dot string by Decimal Format and then To Double by  Double.parseDouble(), and then again to string by BigDecimal.valueOf(d).
So we get 7.0E-9.

Option 4:
Double d= 0.000000007;
DecimalFormat df = new DecimalFormat("#.#################");
d = Double.parseDouble(df.format(d));
System.out.println(newBigDecimal(Double.toString(d)).toPlainString() );

new BigDecimal(String s),Translates the string representation of a BigDecimal into a BigDecimal.The value of the returned BigDecimal is equal to significand × 10 exponent.

This gives the output without scientific notation : 0.0000000070;

Comments

Popular posts from this blog

To increase the size of heap memory for a Netbeans project

Adding/Updating Java projects from existing sources to Netbeans Project