If I have an input string,
input has Three or more decimals I want the string to be handled by
a separate if-loop
.
I've created the following console
program for this purpose, where the if-loop
is entered if (the
first constraint) there is a decimal point, .
in the string and
(the second constraint) if the amount of decimals are Three or
more.
using
System;
namespace Test
{
class
Program
{
static void Main(string[] args)
{
string input = "0.065";
string
output;
Console.WriteLine(input.ToString()); // (1)
Console.WriteLine(input.ToString().IndexOf(".")); // (2)
Console.WriteLine(input.ToString().Substring(input.ToString().IndexOf("."))); //
(3)
if (Convert.ToInt32(input.IndexOf('.')) != -1 &&
Convert.ToInt32(input.Substring(input.IndexOf('.') + 1).Length) >=
3)
{
output = input.Substring(input.IndexOf('.') +
1);
Console.WriteLine(Convert.ToInt32(output));
}
Console.ReadLine();
}
}
}
This
yields the expected result, i.e.
(1)
prints 0.065
,
(2)
prints 1
,
(3) prints
.065
and (4) prints
3
.
My issue is that
when I remove the hard coded string value, input = "0.065"
and
replace it with
input =
Row["Price"].ToString();
where
Row["Price"].ToString();
is a value from a XML file, which also
has value 0.065
I get the following prints from
console:
(1) yields
0.065
(2) yields
-1
(should be 1 as there is a .
present)
(3) yields
0.065
(should be .065
)
and (4) is not printed, because the
if constraints
are not
met.
Does anyone have any idea why this is? I am
new to C#, but I figure that since input
is of type
string
in both cases, there shouldn't be any difference?
Especially as I apply ToString()
to my new string
?
input
No comments:
Post a Comment