Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 14th, 2007, 06:20 PM
skinnybenny skinnybenny is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Posts: 8 skinnybenny User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old December 11th, 2009, 01:01 PM
sentientmachine sentientmachine is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2009
Posts: 2 sentientmachine User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 5 sec
Reputation Power: 0
Post 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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Double with no scientific notation?


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap