Thursday, 30 June 2011

Truncate decimals instead of Rounding from a double value In C#

Introduction

Many times while working on applications, we often land into a situation where we want to truncate the decimal places from a number instead of round like suppose we have 5.329 and I want to truncate the third digit then my required output will be 5.32. Here, I will walk through the different ways which I know in which we can achieve the same.

Approaches

I have created the approaches as making the C# method.

1. Truncate the decimals through the string operations.

Below method only usable if you want to truncate the decimal places to two.

   1: #region Round
   2: /// <summary>
   3: /// Truncate the last digit after two digit floating points.
   4: /// </summary>
   5: /// <param name="value">A double-precision floating-point number to be rounded.</param>
   6: /// <returns>Double value contains two digit floating points.</returns>
   7: public static double Round (double value)
   8: {
   9:   double returnValue = 0;
  10:  
  11:   try
  12:   {
  13:     //Convert the value into the string for round.
  14:     string input = Convert.ToString(value);
  15:  
  16:     //Check if the input contains decimal places
  17:     if (input.Contains("."))
  18:     {
  19:       //Check if there are 3 or more digits after the decimal point.
  20:       if (input.Length - input.IndexOf(".", StringComparison.Ordinal) >= 4)
  21:       {
  22:         //Replace the input value with the required value. Added 3 to get only 2 digits after the decimal point.
  23:         input = input.Substring(0, input.IndexOf(".", StringComparison.Ordinal) + 3); 
  24:       }
  25:     }
  26:         //Converts the string representation of a number to its System.Double equivalent.
  27:     returnValue = Convert.ToDouble(input);
  28:   }
  29:   catch { } //Suppress exception no need to handle.
  30:  
  31:   return returnValue;
  32: }
  33: #endregion

2. Truncate the decimals through the mathematical operations.

Through this method you can truncate any number of decimal places.


   1: #region Round
   2: /// <summary>
   3: /// Rounds a decimal value to a specified number of fractional digits.
   4: /// </summary>
   5: /// <param name="value">A decimal number to be rounded.</param>
   6: /// <param name="decimals">The number of decimal places in the return value.</param>
   7: /// <returns>The number nearest to value that contains a number of fractional digits equal to decimals.</returns>
   8: public static double Round(double value, byte decimals)
   9: {
  10:   double returnValue = 0;
  11:  
  12:   try
  13:   {
  14:     if (value != 0)
  15:     {
  16:       //Gets the power of 10 base on passed decimal parameter value (10^decimals).
  17:       double powValue = Math.Pow(10, decimals);
  18:  
  19:       //Gets the truncated value.
  20:       returnValue = Math.Truncate(CDCommon.ToDouble(value * powValue)) / powValue;
  21:     }
  22:   }
  23:   catch
  24:   { throw; }
  25:  
  26:   return returnValue;
  27: }
  28: #endregion

Conclusion

From both the ways, as per my opinion prefer the second approach in implementation because it’s very light compared to the first in terms of calculations.

1 comment:

  1. Thanks so much for this sample code. I've been trying to figure this out for a while. So very helpful!

    With the new feature of extension methods I've made my copy into an extension method of double. So all I have to do is double.Round(int) and it will return the truncated value. I did change the byte into an int for ease of use.

    Kris

    ReplyDelete