c++ how to get "one digit exponent" with printf

Kris_R picture Kris_R · Jan 7, 2012 · Viewed 16.4k times · Source

Is there a way to print in scientific notation less than 3 places for exponent part of number? The 6.1 formatting doesn't affect exponent but only the number part:

var=1.23e-9;
printf ("%e\n", var);
printf ("%6.1e\n", var);

gives

1.230000e-009
1.2e-009

I've also tried this in wxWidgets with formatting of string but the behavior is the same.

m_var->SetLabel(wxString::Format(wxT("%6.1e"),var));

What I'd like to have is 1.2e-9.

Answer

Ilya Kogan picture Ilya Kogan · Jan 7, 2012

According to Wikipedia:

The exponent always contains at least two digits; if the value is zero, the exponent is 00. In Windows, the exponent contains three digits by default, e.g. 1.5e002, but this can be altered by Microsoft-specific _set_output_format function.

_set_output_format