没人理吗?我自己回去查了下资料,发现其实不是太难。
我自己写了个程序,请各位高手帮忙指正。import javax.swing.JOptionPane;
import java.text.NumberFormat;
import java.util.Locale;public class DoubleToString
{
  public static void main(String[] args)
  {
    //double d=3333333.3333333;  //这个是测试数据
    String strNum=JOptionPane.showInputDialog("请输入一个浮点数:");
    double d=String.valueof(strNum);  /*这句话是错的。我是想把输入的字符串变成double,但不                                     知道怎么搞,是什么方法,请大家帮忙解决*/
    NumberFormat number=NumberFormat.getNumberInstance();  //设置为默认的模式
    number.setMaximumFractionDigits(8);  //设置格式化输出显示的最多显示小数位为8位
    System.out.println(number.format(d));//格式化输出
    System.exit(0);  //用了弹出窗口后的结束线程
  }
}欢迎大家讨论

解决方案 »

  1.   

    好象没有强制转换吧,你的=号个两边类型不同啊!
    我决的应该用case引证:通过变量将每种情况扫入变量中,在由变量的值去确定string
      

  2.   

    你程序写错了:>
     double d=Double.parseDouble(strNum);
      

  3.   

    如果你感觉NumberFormat不好用,可以继承Formater类,自己写。
      

  4.   

    NumberFormat number=NumberFormat.getNumberInstance();  //设置为默认的模式
        number.setMaximumFractionDigits(8);
      

  5.   

    /*
     * Created on 2005-2-12
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package test;import javax.swing.JOptionPane;/**
     * @author SquallStar
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class Test {

    public static String first(String s){
    char[] a = s.toCharArray();
    int i = 0;
    int j = 0;
    boolean allz = false;

    for(i = 0; i < a.length; ++i){
    if(a[i] == '.'){
    j = i;
    break;
    }
    }
    ++i;
    for(;i < a.length; ++i){
    if(a[i] == '0'){
    allz = true;
    }else {
    allz = false;
    break;
    }
    }

    if(allz)
    return new String(a, 0, j);
    else
    return s;
    }

    public static String second(String s){
    System.out.println(s);
    char[] a = s.toCharArray();
    int i = 0;
    int fi = 0;
    int add = 0;
    boolean hasp = false;
    boolean haso = false;
    String first = "";
    String last = "";
    String result = "";

    if(a[0] == '+' || a[0] == '-'){
    first = String.valueOf(a[0]);
    haso = true;
    }
    for( ; i < a.length; ++i){
    if(a[i] == '.'){
    hasp = true;
    break;
    }
    }
    System.out.println(i);
    if(hasp){
    last = new String(a, i , a.length - i);
    --i;
    }
    if(!hasp)
    --i;


    result = last;

    if(haso){
    fi = 1;
    }

    for(;i >= fi; --i){
    ++add;
    if(add == 3 && i != fi){
    result = "," + String.valueOf(a[i]) + result;
    add = 0;
    } else {
    result = String.valueOf(a[i]) + result;
    }
    }
    result = first + result;
    return result;
    }

    public static String doubleToString(double dou){
    return Double.toString(dou);
    }

    public static void main(String[] args) {
    String strNum = JOptionPane.showInputDialog("请输入一个浮点数:");
    double d = Double.parseDouble(strNum);
    String first = first(doubleToString(d));
    String result = second(first);
    System.out.println(result);
    }
    }
      

  6.   

    同意!!~samwise2(张光于) 
    double d = Double.parseDouble(strNum);
      

  7.   

    谢谢各位的热情参与。Squall1009(钰枫)兄的比较长,晚上来网吧抄回去慢慢看。顺便再问大家个问题,问完后就此结贴,决不欠大家的分^_^
    public class aa
    {
      public static void main(String[] args)
      {
      

  8.   

    谢谢各位的热情参与。Squall1009(钰枫)兄的比较长,晚上来网吧抄回去慢慢看。顺便再问大家个超简单新手问题,问完后就此结贴,决不欠大家的分^_^
    public class aa
    {
      public static void main(String[] args)
      {
         System.out.println(10.1-10.0);
       }
    }
    上面这个贴没写完不知道怎么就发出去了。可以发现结果不是0。1。多余的精度是哪里来的??哪位可以解释下吗?我看的入门书我翻了下,没找着答案。*_*
     
      

  9.   

    再把我原来问题的题目也写出来,是E文的,大伙看看,我理解错了没?Given a float/double object, convert it to a string representation with thousands and decimal separators. For instance: String floatToString(float value)  if given 230991.291, should return the String "230,991.291"if given 200.0, should return the string "200"if given 87238.001, should return the string "87,238.001"if give -23870.0, should return the string "-23,870" Your method should be able to handle all valid 32 bit float values. 
      

  10.   

    用封装类型Float类 它直接有toString方法 看JAVA的源文件吧
      

  11.   

    package test;/**
     * @author SquallStar
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class TestForTen { public static void main(String[] args) {
    double i = 0;
    for( i = 9.5; i != 10.0 ; i = i + 0.1)
    {
    System.out.println(i);
    try{
    Thread.sleep(500);
    }catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    }
    }看下循环能不能停好了