Double to String,如何在点后删除0

Iva*_*van 4 java android

可能重复:
如何很好地将浮动类型格式化为String?

我有号码:

Double d1=(Double)0;
Double d2=(Double)2.2;
Double d3=(Double)4;
Run Code Online (Sandbox Code Playgroud)

当我使用String时,我得到0.0,2.2,4.0,但我想看0,2.2,4.我该怎么做?

Hei*_*nzi 17

使用DecimalFormat.formatinsted Double.toString:

Double d1 = (Double)0.0;
Double d2 = (Double)2.2;
Double d3 = (Double)4.0;

// Example: Use four decimal places, but only if required
NumberFormat nf = new DecimalFormat("#.####");

String s1 = nf.format(d1); // 0
String s2 = nf.format(d2); // 2.2
String s3 = nf.format(d3); // 4
Run Code Online (Sandbox Code Playgroud)

你甚至不需要Doubles,doubles会工作得很好.