G
Guest
Hi
Have a method that rounds a currency value to the closest quarter. It works
fine as long as the value is above zero, below zero it rounds to the upper
integer. Any working solution is much appreciated.
protected void Page_Load(object sender, EventArgs e)
{
double d1 = 2.73d;
Response.Write(LevelCurrency(d1).ToString("c")); //Correct
Response.Write("<br/>");
double d2 = -2.73d;
Response.Write(LevelCurrency(d2).ToString("c"));//Incorrect
}
private double LevelCurrency(double price)
{
int totalcost = (int)price;
double d = price - totalcost;
if (d < 0.25d)
return (double)totalcost;
if (d > 0.74d)
return (double)(++totalcost);
return (double)(totalcost + 0.5);
}
Have a method that rounds a currency value to the closest quarter. It works
fine as long as the value is above zero, below zero it rounds to the upper
integer. Any working solution is much appreciated.
protected void Page_Load(object sender, EventArgs e)
{
double d1 = 2.73d;
Response.Write(LevelCurrency(d1).ToString("c")); //Correct
Response.Write("<br/>");
double d2 = -2.73d;
Response.Write(LevelCurrency(d2).ToString("c"));//Incorrect
}
private double LevelCurrency(double price)
{
int totalcost = (int)price;
double d = price - totalcost;
if (d < 0.25d)
return (double)totalcost;
if (d > 0.74d)
return (double)(++totalcost);
return (double)(totalcost + 0.5);
}