How can I format a string in Java when I'm not printing it?
Eg. in Python I can do :
s = "%f" % f
All the Java examples I'm seeing online are using printf or some streams. How can I just create a formatted string internally?
How can I format a string in Java when I'm not printing it?
Eg. in Python I can do :
s = "%f" % f
All the Java examples I'm seeing online are using printf or some streams. How can I just create a formatted string internally?
You want String.format
String value = String.format("%.2f", doubleValue);
You can use the String Formatter class:
http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html
public class Format {
public static void main(String[] args) {
String s = String.format("%f ", myfloat);
}
}