String.Format Formatting Options
-
For starters, I am coming from a C/C++ background and I am trying to format a string with String.Format() but the options are somewhat different than that of the conventional printf and its derivatives. Essentially what I want to have happen is I want to print out a numeric value in currency format but I want the number of spaces "before" the decimal place to be fixed. For instance, in the following example I show what I want the output to look like: WHAT I WANT!
Name: Amount Due:
Jack Rolley $1,234.56
Jane Polley $ 843.76WHAT I HAVE BEEN GETTING!
Name: Amount Due:
Jack Rolley $1,234.56
Jane Polley $843.76I have tried using a variety of option with the String.Format() method and I can't seem to find a way to get what I want. Is there a common method (option) that I can use to get the hanging dollar sign? I am using Visual Studio .NET at the student labs here at my university. Any insight into the string formatting issue would be greatly appreciated. Thanks in advance, Luke Martell Kelsen_@hotmail.com
-
For starters, I am coming from a C/C++ background and I am trying to format a string with String.Format() but the options are somewhat different than that of the conventional printf and its derivatives. Essentially what I want to have happen is I want to print out a numeric value in currency format but I want the number of spaces "before" the decimal place to be fixed. For instance, in the following example I show what I want the output to look like: WHAT I WANT!
Name: Amount Due:
Jack Rolley $1,234.56
Jane Polley $ 843.76WHAT I HAVE BEEN GETTING!
Name: Amount Due:
Jack Rolley $1,234.56
Jane Polley $843.76I have tried using a variety of option with the String.Format() method and I can't seem to find a way to get what I want. Is there a common method (option) that I can use to get the hanging dollar sign? I am using Visual Studio .NET at the student labs here at my university. Any insight into the string formatting issue would be greatly appreciated. Thanks in advance, Luke Martell Kelsen_@hotmail.com
Hello Luke, iv checked out this problem and here is exactly what you want. hope it helps. Module Module1 Sub Main() Dim amtDue(1) As Double Dim myString(1) As String amtDue(0) = 1234.56 amtDue(1) = 843.76 myString(0) = Format(amtDue(0), "n") myString(1) = Format(amtDue(1), "n") Console.WriteLine("$" & myString(0).PadLeft(8, " ")) Console.WriteLine("$" & myString(1).PadLeft(8, " ")) Console.ReadLine() End Sub End Module this will display: $1,234.56 $ 843.76 Jordan. III
-
Hello Luke, iv checked out this problem and here is exactly what you want. hope it helps. Module Module1 Sub Main() Dim amtDue(1) As Double Dim myString(1) As String amtDue(0) = 1234.56 amtDue(1) = 843.76 myString(0) = Format(amtDue(0), "n") myString(1) = Format(amtDue(1), "n") Console.WriteLine("$" & myString(0).PadLeft(8, " ")) Console.WriteLine("$" & myString(1).PadLeft(8, " ")) Console.ReadLine() End Sub End Module this will display: $1,234.56 $ 843.76 Jordan. III
me again Hmm.. here is a much more portable way to implement this code, using a function i made: Module Module1 Sub Main() Dim amtDue(1) As Double amtDue(0) = 1234.56 amtDue(1) = 843.76 Console.WriteLine(PadAmt(amtDue(0))) Console.WriteLine(PadAmt(amtDue(1))) Console.ReadLine() End Sub Function PadAmt(ByVal amt As Double) Dim mystring As String mystring = Format(amt, "n") Return "$" & mystring.PadLeft(8, " ") End Function End Module again, this will display EXACTLY what you wanted, as in my other reply. Please lemme know how it goes, thx. Jordan. III