468,761 Members | 1,751 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,761 developers. It's quick & easy.

printf and format in c#

Hi, how would I translate this to c#?

char ttext[100];
sprintf(ttext,"%2.2i %-.10s %8.3f",1,"Mark",123.24);

[expected output] 01 Mark<6spaces> 123.240

Thanks
Nov 15 '05 #1
1 58739

Hi Mark,

Thank you for posting in the community! My name is Jeffrey, and I will be
assisting you on this issue.

Based on my understanding, you want to format numeric in string in C#.
================================================
First, I want to remind you that sprintf(ttext,"%2.2i %-.10s
%8.3f",1,"Mark",123.24) will output:
"01 Mark<0spaces> 123.240" instead of "01 Mark<6spaces> 123.240"

If you want to get your expected output, you should use:
sprintf(ttext,"%2.2i %-10.10s %8.3f",1,"Mark",123.24);

In C#, you can use String.Format method to format numeric into string.
You can try the following Solution to see if it helps resolve your issue:

string result=String.Format("{0,2:00} {1,-10} {2, 8: #.000}",1,"Mark",
123.24);
Console.WriteLine(result);

It will output your wanted string: "01 Mark<6spaces> 123.240"

String.Format method takes one format parameter which is in the form:
{index[,alignment][:formatString]} .
For more information of each fields, please refer to:
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemstringclassformattopic1.asp

Also, you can find "Standard Numeric Format Strings" and "Custom Numeric
Format Strings" documents in:
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconstandardnumericformatstrings.asp
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconcustomnumericformatstrings.asp

There are also samples in:
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconstandardnumericformatstringsoutputexample.asp
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconcustomnumericformatstringsoutputexample.asp

================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Matt Garman | last post: by
2 posts views Thread by rh00667 | last post: by
1 post views Thread by Joachim Schmitz | last post: by
12 posts views Thread by naunetr | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.