Solved

Convert number to string with leading spaces

Posted on 2011-09-29
1
181 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
1 Comment
 
LVL 74

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now