473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 60386

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Matt Garman | last post by:
Is there a clean, portable way to determine the maximum value of converted numerical fields with printf()-like functions? Doing this at compile-time would be preferable. For example, %i should...
5
by: dank | last post by:
http://msdn2.microsoft.com/en-us/library/tcxf1dw6.aspx describes the integer format modifiers accepted by Microsoft's printf. ...
2
by: rh00667 | last post by:
hi all, seeing a source i found these printf format specifiers (new for me) %m and %n$ i tested and works nice, it seems that they are not documented in man (???) i would appreciate to...
1
by: Joachim Schmitz | last post by:
Is it possible to print a float or double in exponential form with a 0 before the decimal point? E.g. 0.1234567E+00 rather than 1.2345670E-01 (using %14.7E) ? As far as I understand the format...
12
by: naunetr | last post by:
in program below #include <stdio.h> int main() { int id = 123, io = 0123, ih = 0x123; long ld = 1234567L, lo = 01234567l, lh = 0X1234567L; float f = 123.456f; double d = 01234.56789L;
1
by: jerjer | last post by:
i have a question please refer below.. Example: #include <stdio.h> int main() { printf("%6.1f", 123456.123456); printf("\n%6.2f", 123456.123456); printf("\n%6.3f",...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.