M
mdeaver2003
I'm trying to output a double using a precision that
varies, governed by the value of a precision variable.
In C I can do it like this:
double pi = 3.14159;
int prec = 4;
printf( "%.*f", prec, pi ); // 3.1416
The * in "%.*f" says to use the value of the next
argument (must be an int) for the precision. Is there
a numeric format in C# that can do this? The best
workaround I've come up with is:
double pi = 3.14159;
int prec = 4;
StringBuilder sb = new StringBuilder( "#0." );
sb.Append( new string( '0', prec ) ); // sb = "#0.0000"
Console.Write( pi.ToString( sb.ToString() ) ); // 3.1416
Thanks,
Mason Deaver
General Dynamics C4 Systems, VertexRSI
varies, governed by the value of a precision variable.
In C I can do it like this:
double pi = 3.14159;
int prec = 4;
printf( "%.*f", prec, pi ); // 3.1416
The * in "%.*f" says to use the value of the next
argument (must be an int) for the precision. Is there
a numeric format in C# that can do this? The best
workaround I've come up with is:
double pi = 3.14159;
int prec = 4;
StringBuilder sb = new StringBuilder( "#0." );
sb.Append( new string( '0', prec ) ); // sb = "#0.0000"
Console.Write( pi.ToString( sb.ToString() ) ); // 3.1416
Thanks,
Mason Deaver
General Dynamics C4 Systems, VertexRSI