Formatting this number

Anonymous
timer Asked: Jun 9th, 2013
account_balance_wallet $5

Question Description

i would like this tiny bit of help.

i built this little program that calculates stuff(unimportant what it calculates).

but sometimes when its dividing or multiplying big numbers, it displays it in the scientific notification.

like it should display: 10000000(10 million)

but it displays: 1.0E7.

how can i fix this? i dont want it to display it in the scientific notifitcation.

code:

[CODE]if(e.getSource()==b1 && box.getSelectedItem()==fish[8]){

String xp1 = txt1.getText();

int xp2 = Integer.parseInt(xp1);

String xp3 = txt2.getText();

int xp4 = Integer.parseInt(xp3);

double xpleft = xp4 - xp2 ;

double fishleft = xpleft / 350;

JOptionPane.showMessageDialog(null, "XP left: "+"\t"+xpleft+"\n"+box.getSelectedItem()+" left: "+"\t"+fishleft+"\n"+"\n"+"NOTE: always round these numbers."+"\n"+"example: fish left: 1,87 = fish left: 2.", "Fishing Calculator", JOptionPane.INFORMATION_MESSAGE);[/CODE]


Tutor Answer

Codorna
School: Cornell University


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);
}
}

flag Report DMCA
Review

Anonymous
Top quality work from this guy! I'll be back!

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors