## Rounding a value Down

Posted: August 2, 2010 in .Net
Tags:

If you did work with .Net Math classes you may have noted that you can quite easily round value upwards. But what about rounding values down?

For example if you tried to round the value 1.23501 into 2 decimal places using the Round method provided by the Math class as below, the result you will get is 1.24. Meaning .Net had rounded the value up.

1. double dbl = Math.Round(1.23501, 2);

If the value is rounded down it should be 1.23, but in .Net I couldn’t find an easy way to get this done. So I did create a small code to get it done. It is very simple only a method with one statement as shown below.

1. double RoundDown(double Number, int Digits)
2. {
3.     try
4.     {
5.         return Math.Truncate(Number) +
6.             (Math.Floor(Math.Round((Number % 1), Digits) * Math.Pow(10, (Math.Round((Number % 1), Digits).ToString().Length – 3)))) *
7.             (1 / Math.Pow(10, Math.Round((Number % 1), Digits).ToString().Length – 3));
8.     }
9.     catch (Exception)
10.     {
11.         MessageBox.Show("Error ! Please check values.");
12.         return 0.0;
13.     }
14. }

Ohh, I think this is complicated right? Please find a bit of self explanatory code below.

1. double RoundDownExplained(double Number, int Digits)
2. {
3.     try
4.     {
5.         // Get the whole number from the double value.
6.         // For example we’ll take Number = 788.34567 and Digits = 3.
7.         // WholeNumberPart = 788.
8.         double WholeNumberPart = Math.Truncate(Number);
9.         // Getting the decimal part from the doouble value.
10.         // DecimalNumberPart = 0.3457.
11.         double DecimalNumberPart = Math.Round((Number % 1), Digits);
12.         // Finding the number of decimal places so we can multiply by 10 to make it suitable for Floor().
13.         // MultiplicationValue = 3.
14.         int MultiplicationValue = Math.Round((Number % 1), Digits).ToString().Length – 3;
15.         // Create the double value to round down.
16.         // RoundedDownValue = 345.
17.         double RoundedDownValue = Math.Floor(DecimalNumberPart * Math.Pow(10, MultiplicationValue));
18.         // Finding the value to devide the result to make it decimal again.
19.         // DivisionValue = 0.001.
20.         double DivisionValue = 1 / Math.Pow(10, MultiplicationValue);
21.         // Creating the decimal part with rounded down value.
22.         // RoundedDownValue_Float = 0.345.
23.         double RoundedDownValue_Float = RoundedDownValue * DivisionValue;
24.         // Creating the full rounded down value.
25.         // return = 788.345.
26.         return WholeNumberPart + RoundedDownValue_Float;
27.     }
28.     catch (Exception)
29.     {
30.         MessageBox.Show("Error ! Please check values.");
31.         return 0.0;
32.     }
33. }

The complete code of the application I created is below with a screenshot of the application running. Hope this helps to you.

1. using System;
2. using System.Text;
3. using System.Windows.Forms;
4. namespace WindowsFormsApplication1
5. {
6.     public partial class Form1 : Form
7.     {
8.         public Form1()
9.         {
10.             InitializeComponent();
11.         }
12.         double RoundDown(double Number, int Digits)
13.         {
14.             try
15.             {
16.                 return Math.Truncate(Number) +
17.                     (Math.Floor(Math.Round((Number % 1), Digits) * Math.Pow(10, (Math.Round((Number % 1), Digits).ToString().Length – 3)))) *
18.                     (1 / Math.Pow(10, Math.Round((Number % 1), Digits).ToString().Length – 3));
19.             }
20.             catch (Exception)
21.             {
22.                 MessageBox.Show("Error ! Please check values.");
23.                 return 0.0;
24.             }
25.         }
26.         double RoundDownExplained(double Number, int Digits)
27.         {
28.             try
29.             {
30.                 // Get the whole number from the double value.
31.                 // For example we’ll take Number = 788.34567 and Digits = 3.
32.                 // WholeNumberPart = 788.
33.                 double WholeNumberPart = Math.Truncate(Number);
34.                 // Getting the decimal part from the doouble value.
35.                 // DecimalNumberPart = 0.3457.
36.                 double DecimalNumberPart = Math.Round((Number % 1), Digits);
37.                 // Finding the number of decimal places so we can multiply by 10 to make it suitable for Floor().
38.                 // MultiplicationValue = 3.
39.                 int MultiplicationValue = Math.Round((Number % 1), Digits).ToString().Length – 3;
40.                 // Create the double value to round down.
41.                 // RoundedDownValue = 345.
42.                 double RoundedDownValue = Math.Floor(DecimalNumberPart * Math.Pow(10, MultiplicationValue));
43.                 // Finding the value to devide the result to make it decimal again.
44.                 // DivisionValue = 0.001.
45.                 double DivisionValue = 1 / Math.Pow(10, MultiplicationValue);
46.                 // Creating the decimal part with rounded down value.
47.                 // RoundedDownValue_Float = 0.345.
48.                 double RoundedDownValue_Float = RoundedDownValue * DivisionValue;
49.                 // Creating the full rounded down value.
50.                 // return = 788.345.
51.                 return WholeNumberPart + RoundedDownValue_Float;
52.             }
53.             catch (Exception)
54.             {
55.                 MessageBox.Show("Error ! Please check values.");
56.                 return 0.0;
57.             }
58.         }
59.         private void button1_Click(object sender, EventArgs e)
60.         {
61.             try
62.             {
63.                 label1.Text = RoundDown(double.Parse(textBox1.Text), int.Parse(textBox2.Text) + 1).ToString();
64.             }
65.             catch (Exception)
66.             {
67.                 MessageBox.Show("Error ! Please check values.");
68.             }
69.         }
70.         private void button2_Click(object sender, EventArgs e)
71.         {
72.             try
73.             {
74.                 label2.Text = RoundDownExplained(double.Parse(textBox1.Text), int.Parse(textBox2.Text) + 1).ToString();
75.             }
76.             catch (Exception)
77.             {
78.                 MessageBox.Show("Error ! Please check values.");
79.             }
80.         }
81.     }
82. }