I'm trying to get double values from Excel to my C# project. But, when I try that, Visual Studio automatically erases the comma/period, and shows the number without them.
For Example:
Excel: 192,168 or 192.168
C#: 192168,00
How can I prevent this from happening?
Excel Get-Value Code:
for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
for (int j = 0; j < dataGridView1.Rows.Count - 1; j++)
{
tsp.addArray(Convert.ToDouble(ds.Tables["Deneme"].Rows[i][j].ToString()), i, j);
}
}
Replace is good way to do it i guess.
Just try like that.
string oldString = ds.Tables["Deneme"].Rows[i][j].ToString(); // Old string in addArray.
string convertedString = oldString.Replace(".",","); // Replacer for dots.
double convertedDouble = Convert.ToDouble(convertedString); // Convert double now.
I think your culture is tr-TR that's why your NumberDecimalSeparator is , not .
That's why when you write 192.168 in your program, it read its as hundred and ninety-two thousand ... but when you write 192,168 hundred and ninety-two ...
If your excel cell is like 192,168, there is no problem. Convert.ToDouble works exactly as you want;
string s = "192,168";
double d = Convert.ToDouble(s, CultureInfo.GetCultureInfo("tr-TR"));
Console.WriteLine(d); //192,168
But if your excel cell is like 192.168, then you need to use n0 format like;
string s = "192.168";
double d = 0;
if(s.Contains("."))
{
d = Convert.ToDouble(s, CultureInfo.GetCultureInfo("tr-TR"));
}
Console.WriteLine(d.ToString("n0")); //192.168
Related
I am planning to get an array of the averages of each column.
But my app crashes at sum[j] += int.Parse(csvArray[i,j]); due to a FormatException. I have tried using Convert.ToDouble and Double.Parse but it still throws that exception.
The increments in the for loop start at 1 because Row 0 and Column 0 of the CSV array are strings (names and timestamps). For the divisor or total count of the fields that have values per column, I only count the fields that are not BLANK, hence the IF statement. I think I need help at handling the exception.
Below is the my existing for the method of getting the averages.
public void getColumnAverages(string filePath)
{
int col = colCount(filePath);
int row = rowCount(filePath);
string[,] csvArray = csvToArray(filePath);
int[] count = new int[col];
int[] sum = new int[col];
double[] average = new double[col];
for (int i = 1; i < row; i++)
{
for (int j = 1; j < col; j++)
{
if (csvArray[i,j] != " ")
{
sum[j] += int.Parse(csvArray[i,j]);
count[j]++;
}
}
}
for (int i = 0; i < average.Length; i++)
{
average[i] = (sum[i] + 0.0) / count[i];
}
foreach(double d in average)
{
System.Diagnostics.Debug.Write(d);
}
}
}
I have uploaded the CSV file that I use when I test the prototype. It has BLANK values on some columns. Was my existing IF statement unable to handle that case?
There are also entries like this 1.324556-e09due to the number of decimals I think. I guess I have to trim it in the csvToArray(filePath) method or are there other efficient ways? Thanks a million!
So there are a few problems with your code. The main reason for your format exception is that after looking at your CSV file your numbers are surrounded by quotes. Now I can't see from your code exactly how you convert your CSV file to an array but I'm guessing that you don't clear these out - I didn't when I first ran with your CSV and experienced the exact same error.
I then ran into an error because some of the values in your CSV are decimal, so the datatype int can't be used. I'm assuming that you still want the averages of these columns so in my slightly revised verion of your method I change the arrays used to be of type double.
AS #musefan suggested, I have also changed the check for empty places to use the IsNullOrWhiteSpace method.
Finally when you output your results you receive a NaN for the first value in the averages column, this is because when you don't take into account that you never populate the first position of your arrays so as not to process the string values. I'm unsure how you'd best like to correct this behaviour as I'm not sure of the intended purpose - this might be okay - so I've not made any changes to this for the moment, pop a mention in the comments if you want help on how to sort this!
So here is the updated method:
public static void getColumnAverages(string filePath)
{
// Differs from the current implementation, reads a file in as text and
// splits by a defined delim into an array
var filePaths = #"C:\test.csv";
var csvArray = File.ReadLines(filePaths).Select(x => x.Split(',')).ToArray();
// Differs from the current implementation
var col = csvArray[0].Length;
var row = csvArray.Length;
// Update variables to use doubles
double[] count = new double[col];
double[] sum = new double[col];
double[] average = new double[col];
Console.WriteLine("Started");
for (int i = 1; i < row; i++)
{
for (int j = 1; j < col; j++)
{
// Remove the quotes from your array
var current = csvArray[i][j].Replace("\"", "");
// Added the Method IsNullOrWhiteSpace
if (!string.IsNullOrWhiteSpace(current))
{
// Parse as double not int to account for dec. values
sum[j] += double.Parse(current);
count[j]++;
}
}
}
for (int i = 0; i < average.Length; i++)
{
average[i] = (sum[i] + 0.0) / count[i];
}
foreach (double d in average)
{
System.Diagnostics.Debug.Write(d + "\n");
}
}
I have to read and write an excel file.
The problem is that when a PC is the decimal separator, (comma) also in the excel file numbers with the comma will be saved.
I would like to save the values for all modes with the point instead of a comma.
Here is a piece of code:
var wb = openWorkBook(filename);
var ws = wb.Worksheet("CNF");
IXLRow row = ws.Row(device.Ordinal - 1 + FirstRow);
for (int j = 0; j < MAXCOLS; ++j)
{
IXLCell cell = row.Cell(j + FirstCol);
cell.Value = Convert.ChangeType(device[j], m_column_type[j]);
}
Convert.ChangeType(object, Type) uses the thread's current culture for conversion. It sounds like you want the invariant culture, so you should use Convert.ChangeType(object, Type, IFormatProvider):
cell.Value = Convert.ChangeType(device[j], m_column_type[j],
CultureInfo.InvariantCulture);
I need to parse this string
"+CMGL: 1,\"REC READ\",\"+420731177370\",\"\",\"2015/03/21 11:26:10+04\""
and I would like to parse for
id = 1, number = +420731177370, date = 2015/03/21 11:26:10+04\
Could you please help me how to do it without Regex because I have got an old version of micro framework.
My code is
for (int i = 0; i < sentences.Length; i += 2)
{
string[] test = sentences[i].Split(',');
for (int j = 1; j < test.Length; j++)
{
//to do stuff
}
}
to do stuff where i need to replace \"xxxxx\" to xxxx
Perhaps something like this will point you in the right direction. Just be aware that while the code below works well for the string in your original post, should that string change it might not work as well since it's relying on character counts as opposed to regexes.
var Source = "+CMGL: 1,\"REC READ\",\"+420731177370\",\"\",\"2015/03/21 11:26:10+04\"";
var SplitSource = Source.Split(',');
String ID = SplitSource[0].ToString().Remove(0, 6); //good
String Number = SplitSource[2].Replace("\"", ""); //good
String Date = SplitSource[4].Replace("\"", ""); //good
I have a big problem with writing some data to a csv file. I have a lot of measurement values. Every value is described by name, unit, value. So i want to build for every value a column with these three properties.
I want to store it into the csv file like this:
Width Cell Wall Thickness Coarseness Curl-Index etc.
mm mm mg/m % etc.
16,2 3,2 0,000 11,7 etc.
Till now i was coding a header for the names, another for the units and the values (that were previously stored into a string array) i just wrote in one line.
Till now my csv file looks like this:(
Width;Cell Wall Thickness;Coarseness;Curl-Index;etc.
mm;mm;mg/m;%;etc.
16,2;3,2;0,000;11,7;etc.
if it were not many values i wouldn't care about this but there are a lot so when i open the csv file there's the problem that the headers dont fit to the values and units. It's not organized, i cannot match the values to the headers.
I would like everything to be organized in columns. Any help would be strongly appreciated!
That's the code that i have till now:
StreamWriter sw = new StreamWriter("test2.csv");
int RowCount = 3;
int ColumnCount = 4;
string[][] Transfer_2D = new string[RowCount][];
Transfer_2D[0] = new string[3]{"Width", "CWT", "Coarseness", "Curl-Index"};//name of the values
Transfer_2D[1] = new string[3] {"mm", "mm", "mg/m", "%"}; //units
Transfer_2D[2] = new string[3] { TransferData[0], TransferData[1], TransferData[2], TransferData[3] };
for (int i = 0; i < RowCount; i++)
{
for (int j = 0; j < ColumnCount; j++)
{
sw.Write(Transfer_2D[i][j]);//write one row separated by columns
if (j < ColumnCount)
{
sw.Write(";");//use ; as separator between columns
}
}
if (i < RowCount)
{
sw.Write("\n");//use \n to separate between rows
}
}
sw.Close();
}
you can set the string to a fixed length.
example look here: (.NET Format a string with fixed spaces)
int iWantedStringLength = 20;
string sFormatInstruction = "{0,-" + iWantedStringLength.ToString() + "}";
for (int i = 0; i < RowCount; i++)
{
for (int j = 0; j < ColumnCount; j++)
{
sw.Write(String.Format(sFormatInstruction, Transfer_2D[i][j]));//write one row separated by columns
if (j < ColumnCount)
{
sw.Write(";");//use ; as separator between columns
}
}
if (i < RowCount)
{
sw.Write("\n");//use \n to separate between rows
}
}
For CSV work I use http://joshclose.github.io/CsvHelper/, this is a very nice helper class but it would require you to change your working a little bit.
I would advice you create a class to store each entry in and then create a "Mapper" to map it to csv fields. Then you can just pass a collection of objects and your mapping class to the helper and it produces a structured CSV.
There are alot of examples on that page so it should be straight forward for you to work through.
I have below text in richTextBox control.
i want to format text like below text
The RTF box can help you here, the only help using RTF will be using a table as Kosala mentioned.
You may use string operations for that:
int equalPos = 20;
for (int l = 0; l < rtfBox.Lines.Length; l++) {
int i = rtfBox.lines[i].IndexOf('=');
int n = equalPos - i;
if ((i >= 0) && (n > 0)) {
rtfBox.lines[i] = rtfBox.lines[i].Insert(i, new string(' ', n));
}
}
Wrote this from head, so please check for errors.
EDIT:
Ok, here's another one:
for (int l = 0; l < rtfBox.Lines.Length; l++) {
int i = rtfBox.lines[i].IndexOf('=');
if (i >= 0) {
rtfBox.lines[i] = rtfBox.lines[i].Insert(i, "\t");
}
}
rtfBox.SelectAll();
rtfBox.SelectionTabs = new int[] { 100 }; // Find a value big enough!
This is what I would do.(These are manual steps.. :)
1).Open MSWord.
2).Create a table; 2 columns and 5 rows (this is for your text)
3). put the text that you wanted to format in to correct cells of the table
4). Save Word Doc as a rtf file.
5). Open rtf file in notepad(notepad++ is better)
There it is.. Now you can find how it is formatted. Now it should not be hard for you to do it in C#. Good luck.