这个是转换成这样形式的: ------------- 1.2E12testString is 1,200,000,000,000 import java.text.*; public class showDouble { public showDouble() { double testNumber=1.2E12; System.out.println(testNumber); String testString=this.convertDouble(testNumber); System.out.println("testString is "+testString); } public String convertDouble(double number) { String backString=""; NumberFormat nf=NumberFormat.getInstance(); backString=nf.format(number); return backString; } public static void main(String[] args) { showDouble showDouble1 = new showDouble(); } }
/** * Method to replace the double group separator with the null * <pre>example: double:1,110,025.1234------->>1110025.1234 * : double:1.3546456546453E8---->>454654654,345 * </pre> * @param d the double number * @return the string that be replaced */ public static String replaceDoubleSeparator(double d,Locale loc) { if (loc==null) loc = Locale.getDefault(); NumberFormat nf=NumberFormat.getNumberInstance(loc); String s=nf.format(d); //get the locale of current cuntry DecimalFormatSymbols dfs=new DecimalFormatSymbols(loc); System.out.println(dfs.getGroupingSeparator()); //get the group separator of current cuntry //example: china: ","; Germany: "." String groupSeparator=new Character(dfs.getGroupingSeparator()).toString(); String newString=s.replaceAll(groupSeparator,""); return newString; } public static void main(String[] args) { double i=454654654.345; double k=1.3546456546453E8; Locale[] locales = { new Locale("en","US"), new Locale("zh","CN"), new Locale("fr","FR"), new Locale("de","DE") }; for (int j = 0;j < locales.length;j ++ ) { System.out.println(replaceDoubleSeparator(i,locales[j])); System.out.println(replaceDoubleSeparator(k,locales[j])); } }
-------------
1.2E12testString is 1,200,000,000,000
import java.text.*;
public class showDouble
{ public showDouble()
{
double testNumber=1.2E12;
System.out.println(testNumber);
String testString=this.convertDouble(testNumber);
System.out.println("testString is "+testString);
} public String convertDouble(double number)
{
String backString="";
NumberFormat nf=NumberFormat.getInstance();
backString=nf.format(number);
return backString;
}
public static void main(String[] args)
{
showDouble showDouble1 = new showDouble();
}
}
* Method to replace the double group separator with the null
* <pre>example: double:1,110,025.1234------->>1110025.1234
* : double:1.3546456546453E8---->>454654654,345
* </pre>
* @param d the double number
* @return the string that be replaced
*/
public static String replaceDoubleSeparator(double d,Locale loc)
{
if (loc==null)
loc = Locale.getDefault();
NumberFormat nf=NumberFormat.getNumberInstance(loc);
String s=nf.format(d);
//get the locale of current cuntry
DecimalFormatSymbols dfs=new DecimalFormatSymbols(loc);
System.out.println(dfs.getGroupingSeparator());
//get the group separator of current cuntry
//example: china: ","; Germany: "."
String groupSeparator=new Character(dfs.getGroupingSeparator()).toString();
String newString=s.replaceAll(groupSeparator,"");
return newString;
} public static void main(String[] args)
{
double i=454654654.345;
double k=1.3546456546453E8; Locale[] locales =
{
new Locale("en","US"),
new Locale("zh","CN"),
new Locale("fr","FR"),
new Locale("de","DE")
};
for (int j = 0;j < locales.length;j ++ )
{
System.out.println(replaceDoubleSeparator(i,locales[j]));
System.out.println(replaceDoubleSeparator(k,locales[j]));
}
}