I need to know how to round to the nearest quarter and tenth.

Right now i am taking to times finding the difference and then trying to round to the nearest quarter or tenth. Which ever i choose.

I have this for code.

I made a class to call, In my class i have this.

namespace WindowsFormsApplication3
{
    class Time
    {
        public Time(string a)
        {
        }
        


        public string Times(string textBox1, string textBox2)
        {

            DateTime Time1 = DateTime.Parse(textBox1);
            DateTime Time2 = DateTime.Parse(textBox2);
            TimeSpan result = Time2 - Time1;
            int Hours = result.Hours;
            decimal Min = result.Minutes;

            decimal decTime = Hours + (((Min * 100) / 60) / 100);
            decTime = Math.Round(decTime, 2);
            return decTime.ToString();

        } 
     }
}

This is what is in the form itself.

private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if ((textBox6.Text != "") || (textBox7.Text != ""))
            {
                textBox19.Text = new Time("").Times(textBox6.Text, textBox7.Text);
                decimal dec = Convert.ToDecimal(textBox19.Text);

                dec = Math.Floor(dec);
                dec = Math.Round(dec, 2);
                dec = System.Decimal.Add(dec, 0.25m);
                textBox19.Text = dec.ToString();


                if (textBox10.Text != "")
                {
                    decimal Min = Convert.ToDecimal(textBox10.Text);
                    dec = dec - convertToDec(Min);
                    textBox19.Text = dec.ToString();
                }
            }
            else
            {
                MessageBox.Show("Must be a start/stop time");
            }
                
        }
/// <summary>
/// Rounds a number to the closest multiple of another number.
/// </summary>
/// <example>83 rounded to closest 25 would give a value of 75 (83 is closer to 75 than 100)</example>
/// <param name="num">The number to be rounded</param>
/// <param name="closest">The value you wish to round to the closest multiple of</param>
/// <returns>The rounded value</returns>
public int Round(int num, int closest) {
    return (int)Math.Round(((double)num)/closest, 0) * closest;
}

Edited 6 Years Ago by Momerath: n/a

/// <summary>
/// Rounds a number to the closest multiple of another number.
/// </summary>
/// <example>83 rounded to closest 25 would give a value of 75 (83 is closer to 75 than 100)</example>
/// <param name="num">The number to be rounded</param>
/// <param name="closest">The value you wish to round to the closest multiple of</param>
/// <returns>The rounded value</returns>
public int Round(int num, int closest) {
    return (int)Math.Round(((double)num)/closest, 0) * closest;
}

what if the number im trying to round is a decimal.?

Be careful when using Math.Round, if you aren't familiar with how it works you may find some odd results. By default it uses MidPointRounding.ToEven which means that 4.5 would round to 4 not 5 as you might expect. Use the overloaded method to specify the MidPointRounding.AwayFromZero for the traditional rounding.

what if the number im trying to round is a decimal.?

Not sure what you are asking. Math.Round accepts decimals if you want to round to a specific number of digits. If you are asking how do I round a decimal to the closest fraction of number, think powers of ten and use some basic math skills.

Not sure what you are asking. Math.Round accepts decimals if you want to round to a specific number of digits. If you are asking how do I round a decimal to the closest fraction of number, think powers of ten and use some basic math skills.

your right. there was an error in my code. I had a piece of code that rounded to a whole number before trying to round to the nearest quarter or tenth like i wanted.

but thanks for the help none the less.

This question has already been answered. Start a new discussion instead.