title: Format numbers with a maximum number of characters
body: <p>I'm putting numbers in a JTable. If I put a number that's too long, it will truncate with an ellipsis. I'd like to override this behavior, so that the numbers are formatted so that they fit into the cells. This would involve putting certain numbers into scientific notation. I also don't want leading or trailing zeroes; if the number isn't the entire width of the cell, it's acceptable.</p>
  
  <p><a href="http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#substring-int-int-" rel="nofollow"><code>String#substring(int, int)</code></a> doesn't work because that wouldn't work with scientific notation or would lose information <code>0.0000000000000001</code>would become <code>0</code> and not <code>1e-16</code>.</p>
  
  <p><a href="http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#format-java.lang.String-java.lang.Object...-" rel="nofollow"><code>String#format(String, Object...)</code></a> with the <code>%g</code> format doesn't work because it leaves trailing/leading zeroes, and doesn't include the scientific notation in the digit count.</p>
  
  <p>I also looked at <a href="http://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html" rel="nofollow"><code>DecimalFormat</code></a>, but couldn't find anything that allowed setting the number of characters.</p>
  
  <p>A few examples of the intended behavior (with the max number of characters being 11):</p>
  
  <pre><code>3 -&gt; 3
  0.0000000000000001 -&gt; 1e-16
  1234567891011121314 -&gt; 1.234568e18
  3.1415926535897932384626433832 -&gt; 3.141592654
  0.00010001000100010001 -&gt; 0.00010001
  </code></pre>
  
  <p>How could I accomplish this?</p>
  
  <p>Thanks in advance!</p>