Printing doubles of fixed size in Java -


i have matrix follows (e.g):

1 2 1000000000000 3.15 

i want print string in way tabulated, each entry taking 8 places:

1.000000    2.000000 1.00e006    3.150000 

i have been researching decimalformat, numberformat , figured out way of doing specific function coded myself, fails when number written using decimal notation. way of printing double string taking specified number of spaces no matter how written (regular or scientific notation)?

why not this? can uses regex remove sign if want. may need more work.

public static string formatscientific(double val) {     // default case.     string out = string.format("%1.6f", val);      // use scientific notation on values greater 10.      if (val >= 10)         out = string.format("%1.2e", val).replaceall("\\+", "").concat("0");      return out; } 

output:

1.000000 2.000000 1.00e120 3.150000 

edit: should have check val >= 10, not val >= 10^2 oops...


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -