# Convert Boolean to Int64 [Answered]RSS

## 16 replies

Last post Oct 09, 2015 03:00 PM by sreekanth.jonna

• sreekanth.jo...

Member

17 Points

151 Posts

### Convert Boolean to Int64

Oct 04, 2015 09:53 PM|sreekanth.jonna|LINK

Hi,

I trying to convert number to string, but it is giving error as "Input string is not in correct format". Please advice below is my code..

```                    foreach (var item in uniqueEventIds)
{

string _return = string.Empty;
// _return = Convert.ToInt64(1.12862898112862E+17).ToString();  // if i give value directly it is giving expected out : 112862898112862000
_return = Convert.ToInt64(item).ToString(); // if i give var item it is throwing error

}

```

Thanks

• oned_gk

All-Star

52232 Points

15523 Posts

### Re: Convert Boolean to Int64

Oct 04, 2015 10:18 PM|oned_gk|LINK

Use TryParse

```        foreach (var item in uniqueEventIds)
{
string _return = string.Empty;
Int64 i;
if(Int64.TryParse(item, out i))
{
_return = i.ToString();
}
}```

Programming to simplify, don't look for difficult way
Suwandi - Non Graduate Programmer
• Li Wang

Star

9859 Points

974 Posts

Microsoft

### Re: Convert Boolean to Int64

Oct 04, 2015 10:38 PM|Li Wang|LINK

Hi sreekanth,

`string _return = Convert.ToInt64(1.12862898112862E+17).ToString();`

It works fine, but below code will throw exception. Because the converter can't recognition the format of int64.

`string _return = Convert.ToInt64("1.12862898112862E+17").ToString();`

So you need to told the convert the format of the string. Try below code.

`string _return = Int64.Parse("1.12862898112862E+17", System.Globalization.NumberStyles.Any).ToString();string _return = Int64.Parse(item, System.Globalization.NumberStyles.Any).ToString();`

Best Regards,
Wang Li

• sreekanth.jo...

Member

17 Points

151 Posts

### Re: Convert Boolean to Int64

Oct 05, 2015 10:09 AM|sreekanth.jonna|LINK

Hi,

I tried both approaches but it is giving me below error.."The best overload method match for 'long.traparse(string,out long) has a invalid arguments.

Please advice.

Thanks

• Rion William...

All-Star

113010 Points

18360 Posts

MVP

### Re: Convert Boolean to Int64

Oct 05, 2015 10:32 AM|Rion Williams|LINK

This is because the conversion methods are expecting an actual integer-like value to be passed in and when it sees the "E+" to denote your exponential notation, it fails. What you may want to do is indicate you need to parse the value as a floating point number using the System.Globalization.NumberStyles.Float flag :

```var example = "1.12862898112862E+17";
// Parse this value as a floating point number
var yourValue = Int64.Parse(example, System.Globalization.NumberStyles.Float);```

You can see a working example of this here. So a revised version of your function might look like :

```foreach (var item in uniqueEventIds)
{
string _return = Int64.Parse("1.12862898112862E+17",System.Globalization.NumberStyles.Float).ToString();
}```

• sreekanth.jo...

Member

17 Points

151 Posts

### Re: Convert Boolean to Int64

Oct 05, 2015 11:42 AM|sreekanth.jonna|LINK

HI Rion,

thanks for your response, your example fine in fiddle, but when i inculde in my project it is throwing error..here is my entire code..

``` var uniqueEventIds = (from DataRow dr in SelectedRows.Rows select dr["Event Id"]).Distinct();  \\ here it will get all eventid's

foreach (var item in uniqueEventIds)  \\ Iam looping each eventid
{
DataTable EventDetails = (from DataRow dr in SelectedRows.Rows
where dr["Event Id"].ToString() == item.ToString()
select dr).CopyToDataTable();

// string _return = string.Empty;
// _return = Convert.ToInt64(1.12862898112862E+17).ToString();

// string _return = Int64.TryParse(item, System.Globalization.NumberStyles.Any).ToString();

// Example value
var example = item;
// Parse this value as a floating point number
var yourValue = Int64.Parse(example, System.Globalization.NumberStyles.Float);

}```

But i am still getting same error :( Could pls help me

Thanks

• sreekanth.jo...

Member

17 Points

151 Posts

### Re: Convert Boolean to Int64

Oct 05, 2015 11:47 AM|sreekanth.jonna|LINK

I tried below in .net Fiddle i am getting error as "cannot convert to String"

```using System;

public class Program
{
public static void Main()
{
// Example value
//var example = "1.12862898112862E+17";
// Parse this value as a floating point number
//var yourValue = Int64.Parse(example, System.Globalization.NumberStyles.Float);

//Console.WriteLine(yourValue);

var uniqueEventIds = "1.12862898112862E+17";

foreach (var item in uniqueEventIds)
{

var example = item;
var yourValue = Int64.Parse(example, System.Globalization.NumberStyles.Float);
}
}
}```

please advice.

Thanks

• Rion William...

All-Star

113010 Points

18360 Posts

MVP

### Re: Convert Boolean to Int64

Oct 05, 2015 12:28 PM|Rion Williams|LINK

So the first thing to do is to make sure you are getting the values that you expect (i.e. your stringified values in exponential notation), so try placing a breakpoint within the code above and making sure of that :

```// Retrieve your unique events
var uniqueEventIds = SelectedRows.Rows.Select(s => Convert.ToString(s["Event Id"])).Distinct();

// Place a breakpoint here and have a look over your existing values```

If that is the case and they look correct, then you could pull your details using the ID and parse the value as follows :

```DataTable EventDetails = SelectedRows.Rows.FirstOrDefault(s => Convert.ToString(s["Event Id"] == item).CopyToDataTable();

// Parse this value as a floating point number
var yourValue = Int64.Parse(item, System.Globalization.NumberStyles.Float);```

• Rion William...

All-Star

113010 Points

18360 Posts

MVP

### Re: Convert Boolean to Int64

Oct 05, 2015 12:30 PM|Rion Williams|LINK

#### sreekanth.jonna

I tried below in .net Fiddle i am getting error as "cannot convert to String"

```using System;

public class Program
{
public static void Main()
{
// Example value
//var example = "1.12862898112862E+17";
// Parse this value as a floating point number
//var yourValue = Int64.Parse(example, System.Globalization.NumberStyles.Float);

//Console.WriteLine(yourValue);

var uniqueEventIds = "1.12862898112862E+17";

foreach (var item in uniqueEventIds)
{

var example = item;
var yourValue = Int64.Parse(example, System.Globalization.NumberStyles.Float);
}
}
}```

please advice.

Thanks

This is because you are attempting to loop through your value, which in this case is a string, so you are going to actually be looping through the characters within the string as opposed to strings within a list.

• sreekanth.jo...

Member

17 Points

151 Posts

### Re: Convert Boolean to Int64

Oct 05, 2015 02:15 PM|sreekanth.jonna|LINK

Hey Rion,

Thanks for you responce, i am getting error "system.datarowcollection does not contain any definition for Select..

• sreekanth.jo...

Member

17 Points

151 Posts

### Re: Convert Boolean to Int64

Oct 05, 2015 02:23 PM|sreekanth.jonna|LINK

Is there any alterantive to solve this ?

• Rion William...

All-Star

113010 Points

18360 Posts

MVP

### Re: Convert Boolean to Int64

Oct 05, 2015 03:45 PM|Rion Williams|LINK

#### sreekanth.jonna

Hey Rion,

Thanks for you responce, i am getting error "system.datarowcollection does not contain any definition for Select..

You might need to explicitly add a using statement for LINQ :

`using System.Linq;`

If that still doesn't work, try using the AsEnumerable() extension on your data table itself (which presumably is SelectedRows) :

```// Retrieve your unique events
var uniqueEventIds = SelectedRows.AsEnumerable().Select(s => Convert.ToString(s["Event Id"])).Distinct();```

If that doesn't work, could you clarify exactly what SelectedRows is. Is it a DataTable? Or something else entirely?

• sreekanth.jo...

Member

17 Points

151 Posts

### Re: Convert Boolean to Int64

Oct 05, 2015 04:08 PM|sreekanth.jonna|LINK

Here is my entire code..

```  DataTable SelectedRows = _datatable.AsEnumerable()   // this will have duplicates eventid as well
.Skip(startDataRow * endDataRow)
.Take(endDataRow - 1)
.CopyToDataTable();

var uniqueEventIds = (from DataRow dr in SelectedRows.Rows select dr["Event Id"]).Distinct();  // this will have only unique id

foreach (var item in uniqueEventIds)
{

var example = item;
var yourValue = Int64.Parse(example, System.Globalization.NumberStyles.Float);
}

```

Selected rows datatable will have data from the excel...but after reading from excel it is converting as ".12862898112862E+17" since it has more than 16 digits.

 Event Id 112862898112862000 112875242112875000 112880670112880000 112882964112882000 112904215112904000
• Rion William...

All-Star

113010 Points

18360 Posts

MVP

### Re: Convert Boolean to Int64

Oct 05, 2015 04:24 PM|Rion Williams|LINK

How are you reading these values from Excel? Additionally, if you have the option to adjust the file that you are reading from, you might try a suggestion like the one mentioned here to avoid the automatic notation changes :

"Just select the column, right click, Format cells, Custom and choose the option that says 0 (second option below General)."

• sreekanth.jo...

Member

17 Points

151 Posts

### Re: Convert Boolean to Int64

Oct 05, 2015 04:29 PM|sreekanth.jonna|LINK

I am using NOPI API and reading the data from excel and below is my code for reading the data from excel..

``` for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);

DataRow dataRow = dt.NewRow();
if (row == null)
{
break;
}
for (int j = row.FirstCellNum; j < cellCount; j++)
{
if (row.GetCell(j) != null)
{
if (row.GetCell(j).CellType == CellType.Formula)
{
dataRow[j] = row.GetCell(j).NumericCellValue;
}
else {
dataRow[j] = row.GetCell(j);
}
}
}

dt.Rows.Add(dataRow);
}
return dt;```

Unfortunately i can't format the excel cells :(

Thanks

• Rion William...

All-Star

113010 Points

18360 Posts

MVP

### Re: Convert Boolean to Int64

Oct 05, 2015 04:38 PM|Rion Williams|LINK

You might consider placing a breakpoint on the following section and see if you can use the Watch window and look to see if there are any properties that might have your number in a non-scientific notation :

```// Get your cell for debugging purposes
var cell = row.GetCell(j);
// Place a breakpoint here
if (cell.CellType == CellType.Formula)
{
dataRow[j] = cell.NumericCellValue;
}
else
{
dataRow[j] = cell;
}```

• sreekanth.jo...

Member

17 Points

151 Posts

### Re: Convert Boolean to Int64

Oct 09, 2015 03:00 PM|sreekanth.jonna|LINK

Hi Rion,

I tried above and i am getting {1.12862898112862E+17} in cell variable and when i am trying to converting it to Int64 it is throwing error.

I think it is expecting bool value when trying to convert..

var cell = Convert.Int64(row.GetCell(j));

Can you please advice.

Thanks