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.
- 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.
- double RoundDown(double Number, int Digits)
- {
- try
- {
- return Math.Truncate(Number) +
- (Math.Floor(Math.Round((Number % 1), Digits) * Math.Pow(10, (Math.Round((Number % 1), Digits).ToString().Length - 3)))) *
- (1 / Math.Pow(10, Math.Round((Number % 1), Digits).ToString().Length - 3));
- }
- catch (Exception)
- {
- MessageBox.Show("Error ! Please check values.");
- return 0.0;
- }
- }
-
Ohh, I think this is complicated right? Please find a bit of self explanatory code below.
- double RoundDownExplained(double Number, int Digits)
- {
- try
- {
- // Get the whole number from the double value.
- // For example we'll take Number = 788.34567 and Digits = 3.
- // WholeNumberPart = 788.
- double WholeNumberPart = Math.Truncate(Number);
- // Getting the decimal part from the doouble value.
- // DecimalNumberPart = 0.3457.
- double DecimalNumberPart = Math.Round((Number % 1), Digits);
- // Finding the number of decimal places so we can multiply by 10 to make it suitable for Floor().
- // MultiplicationValue = 3.
- int MultiplicationValue = Math.Round((Number % 1), Digits).ToString().Length - 3;
- // Create the double value to round down.
- // RoundedDownValue = 345.
- double RoundedDownValue = Math.Floor(DecimalNumberPart * Math.Pow(10, MultiplicationValue));
- // Finding the value to devide the result to make it decimal again.
- // DivisionValue = 0.001.
- double DivisionValue = 1 / Math.Pow(10, MultiplicationValue);
- // Creating the decimal part with rounded down value.
- // RoundedDownValue_Float = 0.345.
- double RoundedDownValue_Float = RoundedDownValue * DivisionValue;
- // Creating the full rounded down value.
- // return = 788.345.
- return WholeNumberPart + RoundedDownValue_Float;
- }
- catch (Exception)
- {
- MessageBox.Show("Error ! Please check values.");
- return 0.0;
- }
- }
-
The complete code of the application I created is below with a screenshot of the application running. Hope this helps to you.
- using System;
- using System.Text;
- using System.Windows.Forms;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- double RoundDown(double Number, int Digits)
- {
- try
- {
- return Math.Truncate(Number) +
- (Math.Floor(Math.Round((Number % 1), Digits) * Math.Pow(10, (Math.Round((Number % 1), Digits).ToString().Length - 3)))) *
- (1 / Math.Pow(10, Math.Round((Number % 1), Digits).ToString().Length - 3));
- }
- catch (Exception)
- {
- MessageBox.Show("Error ! Please check values.");
- return 0.0;
- }
- }
- double RoundDownExplained(double Number, int Digits)
- {
- try
- {
- // Get the whole number from the double value.
- // For example we'll take Number = 788.34567 and Digits = 3.
- // WholeNumberPart = 788.
- double WholeNumberPart = Math.Truncate(Number);
- // Getting the decimal part from the doouble value.
- // DecimalNumberPart = 0.3457.
- double DecimalNumberPart = Math.Round((Number % 1), Digits);
- // Finding the number of decimal places so we can multiply by 10 to make it suitable for Floor().
- // MultiplicationValue = 3.
- int MultiplicationValue = Math.Round((Number % 1), Digits).ToString().Length - 3;
- // Create the double value to round down.
- // RoundedDownValue = 345.
- double RoundedDownValue = Math.Floor(DecimalNumberPart * Math.Pow(10, MultiplicationValue));
- // Finding the value to devide the result to make it decimal again.
- // DivisionValue = 0.001.
- double DivisionValue = 1 / Math.Pow(10, MultiplicationValue);
- // Creating the decimal part with rounded down value.
- // RoundedDownValue_Float = 0.345.
- double RoundedDownValue_Float = RoundedDownValue * DivisionValue;
- // Creating the full rounded down value.
- // return = 788.345.
- return WholeNumberPart + RoundedDownValue_Float;
- }
- catch (Exception)
- {
- MessageBox.Show("Error ! Please check values.");
- return 0.0;
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- label1.Text = RoundDown(double.Parse(textBox1.Text), int.Parse(textBox2.Text) + 1).ToString();
- }
- catch (Exception)
- {
- MessageBox.Show("Error ! Please check values.");
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- try
- {
- label2.Text = RoundDownExplained(double.Parse(textBox1.Text), int.Parse(textBox2.Text) + 1).ToString();
- }
- catch (Exception)
- {
- MessageBox.Show("Error ! Please check values.");
- }
- }
- }
- }
-
No comments:
Post a Comment