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. How to align decimal value while writing to text file in vb.net

How to align decimal value while writing to text file in vb.net

Scheduled Pinned Locked Moved Visual Basic
tutorialquestioncsharphelp
4 Posts 3 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.
  • A Offline
    A Offline
    Andraw Tang
    wrote on last edited by
    #1

    Hi, dear all I need to create a text file with each control at a line, in this line, it include control value, control name and description, the output should be as the following: False Print - XXXXXXXXXXXXXXXXXXXXXXXXXX 2 Contrl 1 - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 3.6 Control 2 - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 23.8 Control 3 - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 0.005 Control 4 - XXXXXXXXXXXXXXXXXXXXXX The problem is align the column 1, how can I align the decimal point at same position at each line, and prefix the heading as space? and the heading space number is changed according the value? For example, for contrl 1 and control 2, the heading space is 3, but for control 3, it's 2. I use something like: value = Format(Contrl1, " 0") Writer.WriteLine(value.PadRight(ValueFieldLen) & _ "Contrl 1".PadRight(ContrlFieldLen) & _ "- XXXXXXXXXXXXXXXXXXXXXXXXXX") value = Format(Contrl3, " 0.0") Writer.WriteLine(value.PadRight(ValueFieldLen) & _ "Control 3".PadRight(ContrlFieldLen) & _ "- XXXXXXXXXXXXXXXXXXXXXXXXXX") In this way, the prefix spacing is fixed, this isnot I want. Thanks!

    D L 2 Replies Last reply
    0
    • A Andraw Tang

      Hi, dear all I need to create a text file with each control at a line, in this line, it include control value, control name and description, the output should be as the following: False Print - XXXXXXXXXXXXXXXXXXXXXXXXXX 2 Contrl 1 - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 3.6 Control 2 - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 23.8 Control 3 - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 0.005 Control 4 - XXXXXXXXXXXXXXXXXXXXXX The problem is align the column 1, how can I align the decimal point at same position at each line, and prefix the heading as space? and the heading space number is changed according the value? For example, for contrl 1 and control 2, the heading space is 3, but for control 3, it's 2. I use something like: value = Format(Contrl1, " 0") Writer.WriteLine(value.PadRight(ValueFieldLen) & _ "Contrl 1".PadRight(ContrlFieldLen) & _ "- XXXXXXXXXXXXXXXXXXXXXXXXXX") value = Format(Contrl3, " 0.0") Writer.WriteLine(value.PadRight(ValueFieldLen) & _ "Control 3".PadRight(ContrlFieldLen) & _ "- XXXXXXXXXXXXXXXXXXXXXXXXXX") In this way, the prefix spacing is fixed, this isnot I want. Thanks!

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Andraw Tang wrote:

      how can I align the decimal point at same position at each line, and prefix the heading as space? and the heading space number is changed according the value?

      You're going to have to show examples of this. Your examples would have to be posted inside PRE tags, otherwise HTML formatting will remove all spaces and wreck the formatting. If you have a variable number of numbers before and after the decimal point, you'd have to either: 1) Pad both sides of the decimal point with spaces, making the column a fixed width that can contain the maximum number of digits for BOTH sides of the decimal point, 2) Keep track of the number of numbers on both sides of the decimal point and recalculate the fixed width of the column, the position of the decimal point inside the column, and what the maximum padding values show be for both sides of the decimal to keep the decimal point in the same place. This would require that you know all of the values in the column before you write event a single line to the text file.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      1 Reply Last reply
      0
      • A Andraw Tang

        Hi, dear all I need to create a text file with each control at a line, in this line, it include control value, control name and description, the output should be as the following: False Print - XXXXXXXXXXXXXXXXXXXXXXXXXX 2 Contrl 1 - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 3.6 Control 2 - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 23.8 Control 3 - XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 0.005 Control 4 - XXXXXXXXXXXXXXXXXXXXXX The problem is align the column 1, how can I align the decimal point at same position at each line, and prefix the heading as space? and the heading space number is changed according the value? For example, for contrl 1 and control 2, the heading space is 3, but for control 3, it's 2. I use something like: value = Format(Contrl1, " 0") Writer.WriteLine(value.PadRight(ValueFieldLen) & _ "Contrl 1".PadRight(ContrlFieldLen) & _ "- XXXXXXXXXXXXXXXXXXXXXXXXXX") value = Format(Contrl3, " 0.0") Writer.WriteLine(value.PadRight(ValueFieldLen) & _ "Control 3".PadRight(ContrlFieldLen) & _ "- XXXXXXXXXXXXXXXXXXXXXXXXXX") In this way, the prefix spacing is fixed, this isnot I want. Thanks!

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Edit; misread the previous answer, you should go with that :) -- How about creating a small method that takes a decimal and converts it to string? Shouldn't be too hard to add logic to ensure that there are N characters before the decimal-separator, and a fixed amount of characters behind the separators :) That would mean that your decimal-separator gets a fixed position, in a column with a fixed length. If you'd need a dynamic version that adapts, then you'd have to convert all the values in the columns first, to determine the widest numbers.

        I are Troll :suss:

        A 1 Reply Last reply
        0
        • L Lost User

          Edit; misread the previous answer, you should go with that :) -- How about creating a small method that takes a decimal and converts it to string? Shouldn't be too hard to add logic to ensure that there are N characters before the decimal-separator, and a fixed amount of characters behind the separators :) That would mean that your decimal-separator gets a fixed position, in a column with a fixed length. If you'd need a dynamic version that adapts, then you'd have to convert all the values in the columns first, to determine the widest numbers.

          I are Troll :suss:

          A Offline
          A Offline
          Andraw Tang
          wrote on last edited by
          #4

          Eddy, Thank you very much, that what I did right now.

          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