Solved

Convert number to string with leading spaces

Posted on 2011-09-29
1
187 Views
Last Modified: 2013-12-17
// there's got to be an easier better way
string ValueStr;
if (Number < 10)
{
  ValueStr = "  " + Number.ToString();
}
else if (Number < 100)
{
  ValueStr = " " + Number.ToString();
}
else if (Number < 1000)
{
  ValueStr = Number.ToString();
}
else
{
  ValueStr = "---";
}

Open in new window

0
Comment
Question by:deleyd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 36818647
You can do something like this:

string ValueStr = string.Format("{0,3:0}", Number);

Open in new window


Where "{0}" is the placeholder for the first parameter following the format string (in this case, Number), ",3" means right justify the value and make the field a width of 3, and ":0" means format the value as a number, where there is at least one zero. You could also do:

string ValueStr = string.Format("{0,3:##0}", Number);

Open in new window


which does the same thing, and essentially means the same thing, but is more visually indicative of what is going on. The hashes ( # ) mean optional values.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Run tasks synchronously in background thread 19 82
T-SQL and CLR parameter strings 9 66
asp.net, radiobuttonlist, c# 3 69
GET INFO ABOUT WHAT THE PRINTER IS DOING ? 11 76
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question