473,569 Members | 3,009 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 60423

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.F ormat("{0,2:00} {1,-10} {2, 8: #.000}",1,"Mark ",
123.24);
Console.WriteLi ne(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/
frlrfsystemstri ngclassformatto pic1.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/cpconstandardnu mericformatstri ngs.asp
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconcustomnume ricformatstring s.asp

There are also samples in:
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconstandardnu mericformatstri ngsoutputexampl e.asp
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconcustomnume ricformatstring soutputexample. 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
8697
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 never convert to a string that is longer than the number of digits in INT_MAX, right? I'd like to create a structure that contains character...
5
27793
by: dank | last post by:
http://msdn2.microsoft.com/en-us/library/tcxf1dw6.aspx describes the integer format modifiers accepted by Microsoft's printf. http://www.gnu.org/software/libc/manual/html_node/Integer-Conversions.html describes the integer format modifiers accepted by glibc's printf. They differ (no big surprise), but the difference that's bugging me...
2
5699
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 know any document about these, and any other
1
2420
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 specifies in n1256, this isn't possible, is it? Bye, Jojo
12
3543
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
3734
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", 123456.123456);
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.