Skip to content

Instantly share code, notes, and snippets.

@lejon
Created March 31, 2019 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lejon/2de9c0a5425faf2b0000d822f026c7ad to your computer and use it in GitHub Desktop.
Save lejon/2de9c0a5425faf2b0000d822f026c7ad to your computer and use it in GitHub Desktop.
Prints a double array in scientific notation with the same number of digits per index.
public static String toRoundedString(double[] a) {
if (a == null)
return "null";
int iMax = a.length - 1;
if (iMax == -1)
return "[]";
StringBuilder b = new StringBuilder();
b.append('[');
for (int i = 0; ; i++) {
DecimalFormat df = new DecimalFormat("0.00000E0");
String formatted = df.format(a[i]);
b.append(formatted);
if (i == iMax)
return b.append(']').toString();
b.append(", ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment