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:
PS: Why you not have the same issue with ‘8.5’ is that it can exactly be represented in binary form.