用java.text.DecimalFormat:import java.text.DecimalFormat; public class Temp { public static void main(String[] args) { String value = "000000.0000"; //000000.0000 -> 0,000.00 DecimalFormat df = new DecimalFormat(); df.setGroupingUsed(true); df.setMinimumFractionDigits(2); df.setMinimumIntegerDigits(4); System.out.println(df.format(Double.parseDouble(value))); //000000.0000 -> 0,000.0000 df.setGroupingUsed(true); df.setMinimumFractionDigits(4); df.setMinimumIntegerDigits(4); System.out.println(df.format(Double.parseDouble(value))); //0,000.0000 -> 00000000.0000 try { value = "0,000.0000"; df.setGroupingUsed(true); double dValue = df.parse(value).doubleValue(); df.setGroupingUsed(false); df.setMinimumIntegerDigits(8); df.setMinimumFractionDigits(4); System.out.println(df.format(dValue)); } catch(Exception e) { e.printStackTrace(); } } }
TO: qzj() 还是不行:( 报告错误: java.lang.StringIndexOutOfBoundsException at java.lang.StringBuffer.insert(StringBuffer.java:874) at _jsp._test1__jsp._jspService(/test1.jsp:16) at com.caucho.jsp.JavaPage.service(JavaPage.java:89) at com.caucho.jsp.JavaPage.subservice(JavaPage.java:83) at com.caucho.jsp.Page.service(Page.java:334) at com.caucho.server.http.Invocation.service(Invocation.java:255) at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:201) at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:217) at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:151) at com.caucho.server.TcpConnection.run(TcpConnection.java:140) at java.lang.Thread.run(Thread.java:536)
TO:cbhyk() 大哥,您的好意我心领了,可这结果………… 500 Servlet Exception Note: sun.tools.javac.Main has been deprecated. /test.jsp:28: Missing term. try { ^ /test.jsp:28: ';' expected. try { ^ /test.jsp:3: Invalid expression statement. import java.text.DecimalFormat; ^ /test.jsp:3: '}' expected. import java.text.DecimalFormat; ^ /test.jsp:9: Method void main(String[]) can't be static in inner class _jsp._test__jsp. Temp. Only members of interfaces and top-level classes can be static. public static void main(String[] args) ^ /test.jsp:78: Class or interface declaration expected. } catch (java.lang.Throwable e) { ^ 6 errors, 1 warning
StringBuffer b=new StringBuffer();
String s=........;//s--->0,000.0000
int i=0;
while(i<s.length()){
if(s.charAt(i)!=','){
b.append(s.charAt(i));
}
i++;
}
String r=b.toString();//结果。00000.0000->0,000.0000
StringBuffer b=new StringBuffer(s);//s-->0000.0000
int a=s.indexOf(".");
a=a-1;
while(a>0){
a=a-3;
b.insert(a,",")
}
s=b.toString();//结果要上课了,来不及细看。大致方法就是这样把。
我们还年轻牛奶会有的奶牛也会有的
可天天在 csdn 混这些会有吗 ??
public class Temp
{
public static void main(String[] args)
{
String value = "000000.0000"; //000000.0000 -> 0,000.00
DecimalFormat df = new DecimalFormat();
df.setGroupingUsed(true);
df.setMinimumFractionDigits(2);
df.setMinimumIntegerDigits(4);
System.out.println(df.format(Double.parseDouble(value))); //000000.0000 -> 0,000.0000
df.setGroupingUsed(true);
df.setMinimumFractionDigits(4);
df.setMinimumIntegerDigits(4);
System.out.println(df.format(Double.parseDouble(value))); //0,000.0000 -> 00000000.0000
try
{
value = "0,000.0000";
df.setGroupingUsed(true);
double dValue = df.parse(value).doubleValue();
df.setGroupingUsed(false);
df.setMinimumIntegerDigits(8);
df.setMinimumFractionDigits(4);
System.out.println(df.format(dValue));
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
还是不行:(
报告错误:
java.lang.StringIndexOutOfBoundsException
at java.lang.StringBuffer.insert(StringBuffer.java:874)
at _jsp._test1__jsp._jspService(/test1.jsp:16)
at com.caucho.jsp.JavaPage.service(JavaPage.java:89)
at com.caucho.jsp.JavaPage.subservice(JavaPage.java:83)
at com.caucho.jsp.Page.service(Page.java:334)
at com.caucho.server.http.Invocation.service(Invocation.java:255)
at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:201)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:217)
at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:151)
at com.caucho.server.TcpConnection.run(TcpConnection.java:140)
at java.lang.Thread.run(Thread.java:536)
大哥,您的好意我心领了,可这结果…………
500 Servlet Exception
Note: sun.tools.javac.Main has been deprecated.
/test.jsp:28: Missing term.
try {
^
/test.jsp:28: ';' expected.
try {
^
/test.jsp:3: Invalid expression statement.
import java.text.DecimalFormat;
^
/test.jsp:3: '}' expected.
import java.text.DecimalFormat;
^
/test.jsp:9: Method void main(String[]) can't be static in inner class _jsp._test__jsp. Temp. Only members of interfaces and top-level classes can be static.
public static void main(String[] args)
^
/test.jsp:78: Class or interface declaration expected.
} catch (java.lang.Throwable e) {
^
6 errors, 1 warning
直接把那个class运行即可。
double dNumber = 0.00;
DecimalFormat dFormat;
dNumber = Double.parseDouble(formater);
if ((dNumber<0.00001)&&(dNumber>-0.00001)){
dFormat = new DecimalFormat("00000000.0000");
}else{
dFormat = new DecimalFormat("#,##0.0000");
}
return(dFormat.format(dNumber));
}
我试了一下好用。
import java.text.DecimalFormat;
public class TimeTest {public static String setFormat(String formater){
double dNumber = 0.00;
DecimalFormat dFormat;
dNumber = Double.parseDouble(formater);
if ((dNumber<0.00001)&&(dNumber>-0.00001)){
dFormat = new DecimalFormat("00000000.0000");
}else{
dFormat = new DecimalFormat("#,##0.0000");
}
return(dFormat.format(dNumber));
}public static void main(String[] args)
{
String myNumber = "1000.10";System.out.print(setFormat(myNumber)); }
}