tags:

views:

64

answers:

2

have the flowing C++ code:

CString info, info2;
info.Format("%2d", Value[i]);
info2.Format("%4.1f", Value[j]);

want to have the equivalent code in C#

how to do it?

+1  A: 
Value[i].ToString("D");
Value[j].ToString("####.0");
Keith
@Keith, you first statement doesn't specify the minumn 2 digits width. right?
5YrsLaterDBA
+2  A: 

Code ported to C#:

String info;
String info2;
info = String.Format("{0,2:D}", Value[i]);
info2 = String.Format("{0,6:0.0}", Value[j]);

6 is used for aligning the string 4 digits plus decimal point plus decimal digit.

NOTE take care of the current Culture used, you might get , instead of . for some Cultures.

jdehaan
@jdehaan, "{0:D2" will create "01" or " 1" if value is 1?
5YrsLaterDBA
D2 will be 01. And FYI, @jdehaan, you're missing a closing }
Dan Vallejo
Oh damned, you're right! Thanks you both (I made the appropriate changes)
jdehaan