I have this small piece of code:
using System;
using System.Globalization;
namespace project
{
class conditionalStatements
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number greater than 45.2");
string answer = Console.ReadLine();
decimal answer_decimal = Convert.ToDecimal(answer);
// decimal answer_decimal = Decimal.Parse(answer);
decimal compareValue = 45.2m;
Console.WriteLine(answer_decimal);
//prints 452
if(Decimal.Compare(answer_decimal, compareValue) > 0){
// stuff
}
else{
// should enter here
}
}
}
}
The problem is that since both the method Convert.ToDecimal()
and Decimal.Parse()
ignore the dot notation of decimal values (or at least that's what's happening to me) the number is interpreted as 452
instead of 45.2
. No matter how many dots I input. In fact, if I were to enter:
45......2
the converted value still would be converted to 452
. Only if I use the comma, then the converted number is correctly interpreted as 45.2
and I am able to enter the else
condition.
I did not change the NumberFormatInfo.CurrencyDecimalSeparator
. I left it as default '.'