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.
String#substring(int, int)
doesn't work because that wouldn't work with scientific notation or would lose information 0.0000000000000001
would become 0
and not 1e-16
.
String#format(String, Object...)
with the %g
format doesn't work because it leaves trailing/leading zeroes, and doesn't include the scientific notation in the digit count.
I also looked at DecimalFormat
, but couldn't find anything that allowed setting the number of characters.
A few examples of the intended behavior (with the max number of characters being 11):
3 -> 3
0.0000000000000001 -> 1e-16
1234567891011121314 -> 1.234568e18
3.1415926535897932384626433832 -> 3.141592654
0.00010001000100010001 -> 0.00010001
How could I accomplish this?
Thanks in advance!