I'm writing a test for a webpage and I'm replacing some values from the recorded steps with csv values.
The problem is in the record I got this value:
public string UITboxNDocEditText = "866700228007551009280449220150921009003761527571";
and in the csv I got this value:
866700000007551009280449220150921009000001527571
the line I'm using to replace the value is this one:
this.UIMap.InsBDFParams.UITboxNDocEditText = TestContext.DataRow["n_b"].ToString();
the problem is that the script is instead writing this value : 8,66700000007551E+47
I tried casting the TestContext.DataRow line to a string instead of using the .ToString() method like this : (string)TestContext.DataRow["n_b"]; but got an error complaining about casting from a double so I assume the script is considering my value to be a double.
Any tips ?
You can use a format specifier to format the string, F0, the 0 stands for zero decimal places:
this.UIMap.InsBDFParams.UITboxNDocEditText =
((double)TestContext.DataRow["n_b"])
.ToString("F0");
Found out that encapsulating 866700000007551009280449220150921009000001527571 with double quotes solved the issue :
"866700000007551009280449220150921009000001527571".
I think it treats it as a string so it doesn't mess up the numbers.
Related
For example is it possible to do
WriteLine("Statement : {0:c}", 23455);
WriteLine("Statement : {0:n}", 23455);
In one line of code using WriteLine?
To get the output in the format $23,455 you would use {0:c0}, the final 0 tells how many decimal places to show, it defaults to {0:c2} if you don't specify a number.
i'm having problems converting a string to double. I'm using the Double.Parse method but it keeps crashing saying that there is this "Entry character chain with incorrect format". The string I'm trying to convert is this one : 21.00000000. Is it not supported by this function? If so, why not?
This is the line of the error acc.Latitude = double.Parse(accounts.Lat).
Best regards.
It was actually due to regional settings. I've switched the . for , and it worked. Thank you for your help.
You may use Double.TryParse inestad in order to prevent whring input. This method returns true if the input-string can be parsed. In this case the out-param contains the actual double-value:
double value;
if (Double.TryParse(accounts.Lat, out value;))
{
/* do something with the value */
}
This issue should be very simple but I can't find the way to make it work. I have the following code:
string yeah = "0.5";
float yeahFloat = float.Parse(yeah);
MessageBox.Show(yeahFloat.ToString());
But the MessageBox shows "5" instead of "0.5". How can I resolve this using float?
float yeahFloat = float.Parse(yeah, CultureInfo.InvariantCulture);
See documentation: http://msdn.microsoft.com/en-us/library/bh4863by.aspx
0.5 is the way some country are writing decimal number, like in America. In France, decimal number are more written with a comma : 0,5.
Typically, the code you give throw an exception on my computer.
You need to specify from what culture you are expected the string to be parse. If not, it will take your computer culture setting, which is bad, since your code could run in different countries.
So, by specifying an invariant culture, you said to the Parse function : ok, let's try to parse point or comma, try as hard as you can:
string yeah = "0.5";
float yeahFloat = float.Parse(yeah, CultureInfo.InvariantCulture);
Console.Write(yeahFloat);
There is a lot of question already on this subject :
Parse string to float number C#
Best way to parse float?
C# parsing float from string
etc..
By default, Single.Parse(String) parses based on your localization settings. If you want to use a specific one, you'll have to use an appropriate overload of that method with the culture settings that you want.
You can try this with float.TryParse() :
string yeah = "0.5";
float yeahFloat;
if (float.TryParse(yeah,System.Globalization.NumberStyles.Any,
System.Globalization.CultureInfo.InvariantCulture,out yeahFloat))
{
MessageBox.Show(yeahFloat.ToString());
}
Try passing the formatting you require in the ToString() method
MessageBox.Show(yeahFloat.ToString("0.0")); // 1 decimal place
I created a GUI which inputs a value number into another program and stores them in tags. I have a weird conversion problem. When I input 8.5 I get back a string 8.500000000000000... so I use the TryParse and the Double.ToString() method to get around this issue. But the bizarre part is that when I try with 6.9 the string becomes 6.9000000005367431600000.I use 2 dll which might be the cause of this problem. I've read this Decimals explanation but I don't understand how this can be fix my problem. How to get around with this conversion issue?
My Conversion method
private bool TryWrite()
{
// Read() returns a string(which represents int,double, float or string) read from another application
string Oldcopy = _inTouchWrapper.Read(tagNameBox.Text);
// Write() Write my input number into the application stored in a specific tag
_inTouchWrapper.Write(tagNameBox.Text, ValueBox.Text);
//OldCopy is to input back the old copy is the Write (new copy) cannot be performed
string newCopy = _inTouchWrapper.Read(tagNameBox.Text);
if (ValueBox.Text == Double.Parse(newCopy).ToString())
{
return true;
}
else
{
_inTouchWrapper.Write(tagNameBox.Text, Oldcopy);
return false;
}
}
More explanation about the software/and my code. The software has tags ex: Alarm1, TimeString...
Each tag has a type (int, real(a float), string) and a value. This value depends on the type. I cannot input 5.6 if the tag type is int. So I created this method which verifies is the input value can indeed be added in the software. How it works: I input a value in the software, read it back and if the added value matches with the read value, I indeed input it. Else, I input back the oldCopy.
It seems that string "6.9" is not directly converted to double. If is first converted to float and then casted to double. Here is an example:
var f = float.Parse("6.9");
var d = (double)f;
System.Diagnostics.Debug.WriteLine(d.ToString()); //6.90000009536743
PS: Why you not have the same issue with '8.5' is that it can exactly be represented in binary form.
The problem is with the convert of the txt box value, but why?
string strChar = strTest.Substring(0, Convert.ToInt16(txtBoxValue.Text));
Error is: Input string was not in a correct format.
Thanks all.
txtBoxValue.Text probably does not contain a valid int16.
A good way to avoid that error is to use .tryParse (.net 2.0 and up)
int subLength;
if(!int.TryParse(txtBoxValue.Text,out subLength)
subLength= 0;
string strChar = strTest.Substring(0, subLength);
This way, if txtBoxValue.Textdoes not contain a valid number then subLength will be set to 0;
One thing you may want to try is using TryParse
Int16 myInt16;
if(Int16.TryParse(myString, out myInt16)
{
string strChar = strTest.Substring(0, myInt16);
}
else
{
MessageBox.Show("Hey this isn't an Int16!");
}
A couple reasons the code could be faulty.
To really nail it down, put your short conversion on a new line, like this:
short val = Convert.ToInt16(txtBoxValue.Text);
string strChar = strTest.Substring(0, val);
Likely the value in txtBoxValue.Text is not a short (it might be too big, or have alpha characters in it). If it is valid and val gets assigned, then strTest might not have enough characters in it for substring to work, although this normally returns a different error. Also, the second parameter of substring might require an int (not sure, can't test right now) so you may need to actually convert to int32 instead of 16.
What is the value of txtBoxValue.Text during your tests?
ASP.NET offers several validation controls for checking user input. You should use something like a CompareValidator or RegularExpressionValiditor in your WebForm if you're expecting a specific type of input, eg, an Integer.