Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formattting a Double 1

Status
Not open for further replies.

Funkymatt

Programmer
Aug 27, 2002
101
US
I currently have a double: 1.9590447665E10
I would like a double: 19590447665.00

I thought I could use the NumberFormat class but I've been having trouble...I'm somewhat new to JAVA

Best,
Matt
 
Hey Matt,

take a look at the DecimalFormat class.

-pete
[sub]I just can't seem to get back my IntelliSense[/sub]
 
This is still not working. I know that I'm doing something wrong.....?! I would like it always to spit out a long number (no leading zeros) and then my decimal and always two numeric placeholders after the decimal even if there are no pennies in a transaction.

public String formatNumber(double dblNumberIn) throws Exception{
try{
DecimalFormat df1 = new DecimalFormat( "###################.00" ); //Desired format
String strNumberOut = df1.format( dblNumberIn ); //Takes database number and applies the mask above
//should convert a double to ############.## format

return stroutNumber;
}//End Try
catch (Exception e){
System.out.println("formatNumber() function failed within the WriteToFile class. Could not convert double to correct format");
System.out.println(e);
throw e;
}//End Catch
}//End formatNumber method
 
Can you try

Code:
DecimalFormat format = new DecimalFormat("#,##0.00");
PS : Will also put a comma for thousand, million, ...
 
I'd suggest you writing your customized method otherwise the formatting may not work in every situation.Here is the method I created in my own framework.Hope it helps.

public static String format(double nNumber) {
java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
nf.setGroupingUsed(true);
String temp = nf.format(nNumber);
String formatted="";
for (int i = 0; i < temp.length(); i++){
if ( temp.charAt(i) == ',' ) {
formatted += '.';
}else if ( temp.charAt(i) == '.' ) {
formatted += ',';
}else{
formatted += temp.charAt(i);
}
}
return formatted;
}

Salih Sipahi
Software Engineer.
City of Istanbul Turkey
s.sipahi@sahinlerholding.com.tr
 
Hattusas,
In what particular cases would the formatting given by hologram not work? If I pass in a null value? Or extremely large numbers? Just curious. In this case it would be typical for me to run very very large number through this code...100B or perhaps more.
~M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top