Double % formatting question for printf in Java

前端 未结 4 1334
死守一世寂寞
死守一世寂寞 2020-12-14 15:46

%s is a string in printf, and %d is a decimal I thought...yet when putting in

writer.printf(\"%d dollars is the balance of %s\\r\\         


        
4条回答
  •  感情败类
    2020-12-14 16:37

    Yes, %d is for decimal (integer), double expect %f. But simply using %f will default to up to precision 6. To print all of the precision digits for a double, you can pass it via string as:

    System.out.printf("%s \r\n",String.valueOf(d));
    

    or

    System.out.printf("%s \r\n",Double.toString(d));
    

    This is what println do by default:

    System.out.println(d) 
    

    (and terminates the line)

提交回复
热议问题