Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. String.Format Formatting Options

String.Format Formatting Options

Scheduled Pinned Locked Moved Visual Basic
csharpc++visual-studiocomhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kelsen
    wrote on last edited by
    #1

    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.76

    WHAT I HAVE BEEN GETTING!

    Name: Amount Due:
    Jack Rolley $1,234.56
    Jane Polley $843.76

    I 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

    N 1 Reply Last reply
    0
    • K Kelsen

      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.76

      WHAT I HAVE BEEN GETTING!

      Name: Amount Due:
      Jack Rolley $1,234.56
      Jane Polley $843.76

      I 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

      N Offline
      N Offline
      Nadroj
      wrote on last edited by
      #2

      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

      N 1 Reply Last reply
      0
      • N Nadroj

        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

        N Offline
        N Offline
        Nadroj
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups