|
 |
|
Dev Articles Community Forums
> Programming
> Java Development
|
Double with no scientific notation?
Discuss Double with no scientific notation? in the Java Development forum on Dev Articles. Double with no scientific notation? Java Development forum discussing Java, JSP, J2ME. Get help with Java and all of its related languages, libraries and variations. If it’s Java-related, you can discuss it here.
|
|
 |
|
|
|
|

Dev Articles Community Forums Sponsor:
|
|
|

November 14th, 2007, 06:20 PM
|
|
Registered User
|
|
Join Date: Sep 2007
Posts: 8
Time spent in forums: 44 m 19 sec
Reputation Power: 0
|
|
|
Double with no scientific notation?
hey there,
is there a simple way to disable scientific notation when dealing with long decimals? ie i want 0.0000000000000001 to be 0.00000000000001 and not 1Eblahblah. thanks in advance.
ps. im a super novice, i already read about "numberconverter", but idk how to use it basically.
|

December 11th, 2009, 01:01 PM
|
|
Registered User
|
|
Join Date: Dec 2009
Posts: 2
Time spent in forums: 1 h 2 m 5 sec
Reputation Power: 0
|
|
Showing java numbers without scientific notation.
I assume your talking about the double data type which prints the number in scientific notation rather presumptuously.
If you want to use gigantic numbers or very tiny numbers and not deal with scientific notation then run the following code. There is one extra task for you though. You have to be responsible for when and where the rounding occurs. I'm not sure why they turn on the scientific notation by default. It seems the only way to disable it is to specifically set the format picture. But then the drawback is you have to now worry about where to round and how to round.
Run this and you can get huge and tiny decimals showing on screen without scientific notation:
Code:
import java.math.BigInteger;
import java.text.DecimalFormat;
public class Runner
{
public static void main(String[] args)
{
double tiny_number = 1425987050479.9998;
System.out.println(formatDecimal(tiny_number, 0));
System.out.println(formatDecimal(tiny_number, 1));
System.out.println(formatDecimal(tiny_number, 2));
System.out.println(formatDecimal(tiny_number, 3));
System.out.println(formatDecimal(tiny_number, 4));
System.out.println(formatDecimal(tiny_number, 5));
System.out.println(formatDecimal(tiny_number, 6));
float higgs_size = 0.00000000000000000001f;
System.out.println("");
System.out.println(round_my_float(higgs_size, 0));
System.out.println(round_my_float(higgs_size, 1));
System.out.println(round_my_float(higgs_size, 2));
System.out.println(round_my_float(higgs_size, 5));
System.out.println(round_my_float(higgs_size, 7));
System.out.println(round_my_float(higgs_size, 17));
System.out.println(round_my_float(higgs_size, 25));
System.out.println(round_my_float(higgs_size, 54));
//If you want even bigger:
BigInteger width_of_universe_in_lightyears = new
BigInteger("3578106562352388479696502619877777" +
"777777777777777751235237890123456890");
System.out.println("");
System.out.println(width_of_universe_in_lightyears );
}
public static String formatDecimal(double d, int precision)
{
String myformat = "###,###,###,###,##0";
if (precision == 0)
{
//System.out.println("OK Decimal is: " + d);
DecimalFormat df = new DecimalFormat(myformat);
return df.format(d);
}
myformat = "###,###,###,###,##0.";
for(int x= 0; x < precision; x++)
myformat = myformat + "0";
DecimalFormat df = new DecimalFormat(myformat);
return df.format(d);
}
public static String round_my_float(float f, int places)
{
String precision = "";
if (places == 0)
precision = "0";
else
precision = "0.";
for(int y = 0; y < places; y++)
precision += "0";
//DecimalFormat df=new DecimalFormat("0.000");
DecimalFormat df=new DecimalFormat(precision);
return df.format(f);
}
}
Here is the console output of the above code:
1,425,987,050,480
1,425,987,050,480.0
1,425,987,050,480.00
1,425,987,050,480.000
1,425,987,050,479.9998
1,425,987,050,479.99980
1,425,987,050,479.999800
0
0.0
0.00
0.00000
0.0000000
0.00000000000000000
0.0000000000000000000100000
0.000000000000000000009999999682655225000000000000 000000
35781065623523884796965026198777777777777777777777 51235237890123456890
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|