473,407 Members | 2,312 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,407 software developers and data experts.

C# Format Specifiers Help.

I am looking for a way in C# to format my output. I want
to be able to specify a disply width and then have the
variable printed at minimum with that width.

I can accomplish this is C and C++ as follows:

C - printf("%5d", someInt);

C++ - cout << setw(5) << someInt;

What is the C# equivalent? I have been trying:

Console.Write("{0:D5}", someInt); - This however has
leading zeros placed in it.

Thanks for your Help.

Nov 15 '05 #1
2 15923
Kevin,
What is the C# equivalent? I have been trying:

Console.Write("{0:D5}", someInt); - This however has
leading zeros placed in it.


You can do

Console.WriteLine(String.Format("{0:D}", someInt).PadLeft(5));

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #2
Thank you for your reply.

I have also found that the following works:

Console.Write("{0,5:G}", someInt);

Thanks Again.
-----Original Message-----
Kevin,
What is the C# equivalent? I have been trying:

Console.Write("{0:D5}", someInt); - This however has
leading zeros placed in it.
You can do

Console.WriteLine(String.Format("{0:D}", someInt).PadLeft

(5));
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
.

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Peter Ammon | last post by:
Does anyone ever write the arguments to printf()-like functions under the corresponding format specifier? printf("My name is %s and I am %u years old\n", name, age); If the arguments...
5
by: siliconwafer | last post by:
Hi All, What does a 'format specifier' do? Suppose I do, int a = 43; //decimal number printf("%x",a); I will get hex equivalent of 43. Does the format specifier do an implicit "decimal to hex"...
6
by: Udi | last post by:
Hi, I tried looking for it in the documentation but with no luck. What's the equivalent C control strings in C# for the following: "%3d" ? (space padding) "%-3d" ? (left alignment) "%+3d" ?...
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...
0
by: John Mott | last post by:
Hi all, I've developed an interactive utility that can assist in the development of custom datetime format specifier strings. In addition to enumerating the options it will show how a combined...
1
by: Ayyanar | last post by:
What does the following format specifiers do fscanf( fd, "%d : \"%\"",..../.);
1
ashitpro
by: ashitpro | last post by:
i was looking for implementation of gcvt function.. inside that i found out following line sprintf (buf, "%-#.*g", ndigits, value); here ndigite is of type int and value is of type double forget...
2
by: almurph | last post by:
Hi, Hope that you can help me with this. I'm calling DateTime.Now and storing in a DateTime container. I see that the pattern format is: m/ dd/yyyy I need it to be in the format: yyyy-MM-dd...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.