Solved

Convert number to string with leading spaces

Posted on 2011-09-29
1
189 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

622 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