Reputation: 333
Assume there is some class
public class Currency {
private double value;
public double getValue() {
return value;
}
public Currency(double value) {
this.value = value;
}
@Override
public String toString() {
return value + "";
}
}
The point is:
There is float variable
double var = 10.000;
and object
Currency cur = new Currency(var);
I need that method toString() must return "10.000" just as var looks like(if there is sequence of zeros after decimal point suppose that it must return 3 zeros after decimal point), but it returns "10.0"
Upvotes: 1
Views: 1233
Reputation: 7870
Use String.format
:
double a = 10;
String s = String.format("%.3f", a);
System.out.println(s);
Here is the document.
Upvotes: 3
Reputation: 49803
Once assigned to a double, the exact wording of the literal used to specify the value is lost, and just its value is retained. If you need the exact formatting, you should represent it some other way (for example, as a string). If you need a specific number of decimal places, then format your string to have that many.
Upvotes: 0