title: How to Java String.format with a variable precision?
body: <p>I'd like to vary the precision of a double representation in a string I'm formatting based on user input. Right now I'm trying something like:</p>
  
  <pre><code>String foo = String.format("%.*f\n", precision, my_double);
  </code></pre>
  
  <p>however I receive a <code>java.util.UnknownFormatConversionException</code>. My inspiration for this approach was C printf and <a href="http://sharkysoft.com/archive/printf/docs/javadocs/lava/clib/stdio/doc-files/specification.htm" rel="noreferrer">this resource</a> (section 1.3.1). </p>
  
  <p>Do I have a simple syntax error somewhere, does Java support this case, or is there a better approach?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I suppose I could do something like:</p>
  
  <pre><code>String foo = String.format("%." + precision + "f\n", my_double);
  </code></pre>
  
  <p>but I'd still be interested in native support for such an operation.</p>