Reputation: 10055
Sorry for the title but cant really think of a way to phrase it!
Anyway I want to create a string of numbers where the space between the numbers is used to hold the negative symbol if it is required.
Eg.
00003232 00002132-00000545 00646545-00000465
To do this im using the formatting {0: #00000000;-#00000000}
This works but my string is pretty long so I will need a lot of the above formatting types with varying numbers of zeros.
Is there a better way?
Upvotes: 0
Views: 985
Reputation: 159
I used the following and seemed to work:
int[] test = { 3232, 2132, -545, 646545, -465 };
StringBuilder resultString = new StringBuilder();
test.ToList().ForEach(i =>
{
string delimeter = i>=0?" ":"-";
string value = Math.Abs(i).ToString("00000000");
resultString.AppendFormat("{0}{1}", delimeter, value);
});
Console.WriteLine(resultString.ToString());
Upvotes: 0
Reputation: 2409
I think I've done something similar and the article below helped me. I think the model you want would be something like
string.Format("{0:D8}", number)
if not something very close to that. Below is the link and info I"m basing my answer on.
In reply to your comment, I ran this test (which passes)
[Test()]
public void TestNegativePadding()
{
int number1 = 123;
int number2 = -123;
Assert.AreEqual(" 00000123", string.Format("{0,9:D8}", number1));
Assert.AreEqual("-00000123", string.Format("{0,9:D8}", number2));
string str = "";
int[] test = {3232, 2132, -545, 646545, -465};
foreach (var number in test)
{
str += string.Format("{0,9:D8}", number);
}
Assert.AreEqual("00003232 00002132-00000545 00646545-00000465", str.Trim());
}
http://msdn.microsoft.com/en-us/library/dd260048(v=vs.110).aspx
byte byteValue = 254;
short shortValue = 10342;
int intValue = 1023983;
long lngValue = 6985321;
ulong ulngValue = UInt64.MaxValue;
// Display integer values by caling the ToString method.
Console.WriteLine("{0,22} {1,22}", byteValue.ToString("D8"), byteValue.ToString("X8"));
Console.WriteLine("{0,22} {1,22}", shortValue.ToString("D8"), shortValue.ToString("X8"));
Console.WriteLine("{0,22} {1,22}", intValue.ToString("D8"), intValue.ToString("X8"));
Console.WriteLine("{0,22} {1,22}", lngValue.ToString("D8"), lngValue.ToString("X8"));
Console.WriteLine("{0,22} {1,22}", ulngValue.ToString("D8"), ulngValue.ToString("X8"));
Console.WriteLine();
// Display the same integer values by using composite formatting.
Console.WriteLine("{0,22:D8} {0,22:X8}", byteValue);
Console.WriteLine("{0,22:D8} {0,22:X8}", shortValue);
Console.WriteLine("{0,22:D8} {0,22:X8}", intValue);
Console.WriteLine("{0,22:D8} {0,22:X8}", lngValue);
Console.WriteLine("{0,22:D8} {0,22:X8}", ulngValue);
// The example displays the following output:
// 00000254 000000FE
// 00010342 00002866
// 01023983 000F9FEF
// 06985321 006A9669
// 18446744073709551615 FFFFFFFFFFFFFFFF
//
// 00000254 000000FE
// 00010342 00002866
// 01023983 000F9FEF
// 06985321 006A9669
// 18446744073709551615 FFFFFFFFFFFFFFFF
// 18446744073709551615 FFFFFFFFFFFFFFFF
Upvotes: 1