How to align decimal value while writing to text file in vb.net
-
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!
-
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!
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 -
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!
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:
-
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:
Eddy, Thank you very much, that what I did right now.