473,320 Members | 1,952 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,320 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 60365

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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.