I have created a simple age calculator. I want to remove decimal places but the problem is when I substract now
to bday
.
example : I input 2012
, 10
and 23
and the date now is 2014-10-22
,
So when 2014.1022 - 2012.1023
the result would be 1.9999...
I want to remove all decimal places and remain the whole number 1
, but the time I use String.Format("{0:00}"
It rounds off the result to 02
even when I use ConvertToInt32
, I don't want to use split string it needs a lot of code.
Any Ideas?
static void Main(string[] args)
{
string year, month, day = string.Empty;
Console.WriteLine("Enter your Birthdate:");
Console.WriteLine("Year :");
year = Console.ReadLine();
Console.WriteLine("Month :");
month = Console.ReadLine();
Console.WriteLine("Day :" );
day = Console.ReadLine();
try
{
DateTime date = Convert.ToDateTime(year + "-" + month + "-" + day);
var bday = float.Parse(date.ToString("yyyy.MMdd"));
var now = float.Parse(DateTime.Now.ToString("yyyy.MMdd"));
if (now < bday)
{
Console.WriteLine("Invalid Input of date");
Console.ReadLine();
}
Console.WriteLine("Your Age is " + (String.Format("{0:00}", (now - bday)))); //it rounds off my float
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadLine();
}
}
}