Reputation: 3294
I'm new to DDT and I've created a very small test with integer and double values. I can parse the integers without any problems but I can't get use the double.
Here's my C# code:
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "bounds.csv", "bounds#csv",
DataAccessMethod.Sequential)]
[DeploymentItem("bounds.csv")]
[DeploymentItem("schema.ini")]
[TestMethod]
public void GetBestUsingBounds()
{
// https://msdn.microsoft.com/en-us/library/ee624082.aspx
var x1 = Convert.ToDouble(TestContext.DataRow["x1"]);
var x2 = Convert.ToDouble(TestContext.DataRow["x2"]);
}
Here's my csv file:
x1;y1
11,1;55.1
-6;50
And my scheme.ini:
[bounds.csv]
Format=Delimited(;)
The second row is read properly, they're just integers, but the first row is either stripped of the decimals: 11.1 becomes 11 or stripped from the decimal point 11.1 becomes 111.
When I add quotes (double or single) the whole row is skipped.
It doesn't matter if I use schema.ini with ; as delimiter or no scheme.ini with the default , as delimiter.
The problems is not with Convert.ToDouble TestContext.DataRow["x1"] is already wrong.
I'm using VS2013 Pro on a Dutch Win8.1. Any suggestion on how to solve this seemingly easy problem?
Upvotes: 2
Views: 675
Reputation: 1894
It is possible (at least in VS2015) to quote the value, like so:
x1;y1
"11.1";"55.1"
-6;50
It will then be considered a string literal, and it can be converted to double like so:
Convert.ToDouble(TestContext.DataRow["x1"], CultureInfo.InvariantCulture);
Upvotes: 2
Reputation: 35594
The problem is that you cannot parse both 11,1
and 55.1
as doubles in the same locale. For Dutch locale, only comma is accepted.
I would propose using dot always, as it matches invariant culture; in order to ensure that the double separator works as expected, just add
DecimalSymbol=.
in your schema.ini
. (I didn't find a way to explain the CSV parser, which is the locale of the .csv-file; it seems to be a bug.)
Upvotes: 0
Reputation: 1894
This seems to be a bug in MsTest (as Margus pointed out). Here is the workaround I used when I was in the same situation:
Change your csv to
x1;y1
a11.1;a55.1
a-6;a50
and your C# code to
var x1 = Convert.ToDouble(TestContext.DataRow["x1"]ToString().TrimStart('a'));
var x2 = Convert.ToDouble(TestContext.DataRow["x2"]ToString().TrimStart('a'));
Of course any other letter will do the job, too. With the letter prefix, you avoid MsTest converting the csv data to double automatically and thus give it no chance to do it incorrectly.
I know it is ugly, but nobody seems to have had a better answer...
Upvotes: 0
Reputation: 20068
This is a Globalization related bug. The answer you get depends on your locale.
For example:
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
Number number = format.parse("1,234");
double d = number.doubleValue();
Upvotes: 0