I have this data written in the text file "000001010" (it only has this data), but I wanted it when it was for the variable to be 10.10.
I'm reading the data from the file like this:
var linhas = System.IO.File.ReadAllLines(@"C:\teste\pedro.txt");
I have this data written in the text file "000001010" (it only has this data), but I wanted it when it was for the variable to be 10.10.
I'm reading the data from the file like this:
var linhas = System.IO.File.ReadAllLines(@"C:\teste\pedro.txt");
By the rule described: just convert the number to integer and divide by 100.
I used LINQ, so each line of the file will return an item to the valores
list.
var valores = File.ReadAllLines(@"C:\teste\pedro.txt")
.Select(l => (decimal)Convert.ToInt32(l) / 100)
.ToList();
foreach(var val in valores)
Console.WriteLine(val);
Considering that each file will have only one line, it would be better to do this:
var strVal = File.ReadAllLines("C:\teste\pedro.txt")[0];
decimal valor = Convert.ToInt32(strVal) / 100m;
Match the principle that the format is always this and will always be correct can do:
using static System.Console;
using static System.Convert;
public class Program {
public static void Main() {
var campo = "000001010";
decimal valor = ToInt32(campo) / 100M;
WriteLine(valor);
}
}
See running on .NET Fiddle . And no Coding Ground . Also I put GitHub for future reference .
If you have a single element in the file, just read it like this:
File.ReadAllLines("C:\teste\pedro.txt")[0];
As the ReadAllLines()
method returns a array with all the text lines of the file and only have one line just pick up the first element. Do not need to do other manipulation because the line does not contain any other information.