473,386 Members | 1,745 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.

Number formatting with leading spaces

Hi,

what is (if exist) the format string to print an integer in C# with
leading spaces (to do right justification) e.g.

value = 77

|<----->| <--- width = 5 characters
+ 12345 +
| |
| 77 |
^^^
3 leading spaces
Thanks
MrAsm
Apr 4 '07 #1
5 20167
Hello MrAsm,

this should help:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Value:" + formatInt(77, 5));
Console.Read();
}
private static string formatInt(int intVal, int intTotalsize)
{
string retVal = "";
if (intVal.ToString().Length < intTotalsize)
{
for (int slice = 0; slice < intTotalsize - intVal.ToString().Length;
slice++)
{
retVal = retVal + " ";
}
retVal = retVal + intVal.ToString();
}
else
{
retVal = intVal.ToString();
}
return retVal;
}
}
}
Regards

Frank
Hi,

what is (if exist) the format string to print an integer in C# with
leading spaces (to do right justification) e.g.

value = 77

|<----->| <--- width = 5 characters
+ 12345 +
| |
| 77 |
^^^
3 leading spaces
Thanks
MrAsm

Apr 4 '07 #2
On Wed, 4 Apr 2007 16:20:16 +0000 (UTC), Frank Werner
<fr**********************@franke.comwrote:
>Hello MrAsm,

this should help:
Thank you very much for your code, Frank.

MrAsm
Apr 4 '07 #3
"Frank Werner" <fr**********************@franke.comwrote in message
news:5f*************************@news.microsoft.co m...
this should help:
Or, rather than re-inventing the wheel, you could just use this:
http://msdn2.microsoft.com/en-us/lib...g.padleft.aspx
Apr 4 '07 #4
On Apr 4, 11:42 am, MrAsm <m...@usa.comwrote:
On Wed, 4 Apr 2007 16:20:16 +0000 (UTC), Frank Werner

<frank.werner-krippend...@franke.comwrote:
Hello MrAsm,
this should help:

Thank you very much for your code, Frank.

MrAsm
Why not use the String.Format? It's what it is designed for:

Console.WriteLine(String.Format({0,5}, 77));

This is similar to the C printf method. The {0,5} is a placeholder.
The 0 indicating it is the first argument in the list of items to be
formatted (77 in this case). The ,5 indicates that the field is
padded to 5 characters, right justified. For left justified you would
use -5. You can also add an option format string after the 5. See
the following link for more information:

http://msdn2.microsoft.com/en-us/library/fht0f5be.aspx


Apr 4 '07 #5
On 4 Apr 2007 10:29:52 -0700, "Chris Dunaway" <du******@gmail.com>
wrote:
>Why not use the String.Format? It's what it is designed for:

Console.WriteLine(String.Format({0,5}, 77));

This is similar to the C printf method.
[...]
>http://msdn2.microsoft.com/en-us/library/fht0f5be.aspx
Very good! Thank you!

MrAsm
Apr 4 '07 #6

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

Similar topics

0
by: Ellen K. | last post by:
In a previous post I mentioned having a problem getting leading spaces to be written to SQL Server using OPENXML. They showed up fine in my XML documents if I printed them, but not in my database....
2
by: Terry | last post by:
Hi, I am new the Javascript. I was wondering if there is a pre-built or pre-written script that can do simple number formatting. When I do a calculation of 2 floating point numbers, I always...
4
by: ChrisB | last post by:
Hello: I was wondering if there might be a more efficient way to handle number formatting over a range of values: switch(DecimalPlaces) { case 1: formattedResult = result.ToString(".0");...
3
by: SKG | last post by:
i have leading spaces in my database fields. When they appear in the datagrid in browser, they seem to be truncated. I did view source in browser and found that there are indeed spaces but when...
4
by: probashi | last post by:
Hi I want to format numbers with leading zeros Like: Input output 0 00 1 01
0
by: bigbrorpi | last post by:
Hi Is it possible to set global number formatting for an application? I need to have all numbers displayed with thousands separators and that's not currently the case. My desktop location...
4
by: John Nagle | last post by:
One can argue over tab vs. space indentation, but mixing the two is just wrong. Why not have CPython report an error if a file has both leading tabs and leading spaces? I know that was proposed...
0
by: markpringle | last post by:
I am using a very simple multiline textbox (below) to capture text (poems). Many poems require formatting of the first line by including leading spaces. However, on submit, the leading spaces are...
2
Pittaman
by: Pittaman | last post by:
Hello I am creating some crystal reports (for visual studio 2005) based on the content of certain .NET objects. I'm doing this in .NET 2.0. For one of them I'm using a Cross-table to summarize...
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:
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
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
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...
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,...

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.