I have found this vise-versa many times on SO, but never like I want it.
I want to convert a double into a string that takes up exactly n characters.
If n is, for example, 6, then
1,000,123.456 would become 1.00E6
-1,000,123.456 would become 1.0E-6
1.2345678 would become 1.2345
1,000,000,000,000 would become 1.0E12
etc.
How do I achieve that? Regards, Claas M.
P.S How do I set tabs on SO?
If you're limiting you're exponential component to E0 to E99 (for positive exponential) and E0 to E-9 (for negative exponential), you could use a combination of DecimalFormat and Regex to format your results to be 6 characters in length.
Something like:
Results: