venkat
venkat

Reputation: 5738

Getting Exact precision of decimal places from a double in .NET 3.5

Due to a requirement I need very exact precision of a double value to a 4 decimal places as follows:

double myDoubleValue = 50234.9489898997952932;

From the above, I need the output as 50234.9489. I DON'T want Rounding the number in this requirement.

I came across with "Math.Truncate(a * 100) / 100;". But really i'm not interested with this approach.

I'm looking for better approach very simple way something like using String.Format or with Regular Expressions etc.

Upvotes: 1

Views: 7226

Answers (4)

sanguine
sanguine

Reputation: 291

double d = 50234.94895345345345;
var Expected_result =  Double.Parse((Regex.Match(d.ToString(), "[+-]?\\d*.\\d{0,4}")).Value);

Upvotes: 3

Léo
Léo

Reputation: 88

Without regexes:

This works very much fine for any double combinations

using System.Globalization;

class Program
{
    static void Main(string[] args)
    {
        double d = 50234.9489898997952932;
        char probablyDot = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0];
        string[] number = d.ToString().Split(probablyDot);


        //Console.WriteLine(number[0] + probablyDot + number[1].Remove(4));

        Console.WriteLine(number[0] + probablyDot + (number.Length >1 ? (number[1].Length>4? number[1].Substring(0,4):number[1]): "0000"));
        //Output: 50234.9489

        Console.ReadKey();
    }
}

Upvotes: 1

Mark
Mark

Reputation: 487

There are a lot of answers here that work with the input given in the question, but on testing them with a range of values, they all have limitations (see comments).

The only way I can see to achieve this with any decimal input is the following. It might not be a one liner, but it seems robust to me.

private static string TrimDecimalPlaces(double value, int numberOfDecimalPlaces)
{
    string valueString = value.ToString();

    if (!valueString.Contains(".")) return valueString;

    int indexOfDot = valueString.IndexOf(".");

    if ((indexOfDot + numberOfDecimalPlaces + 1) < valueString.Length)
    {
        return valueString.Remove(indexOfDot + numberOfDecimalPlaces + 1);
    }
    return valueString;
}

I've tested this with the following test data and the results are as expected:

  • 1
  • 1.1
  • 1.11
  • 1.111
  • 1.1111
  • 1.11111
  • 1.111111
  • -1
  • -1.1
  • -1.11
  • -1.111
  • -1.1111
  • -1.11111
  • -1.111111

Upvotes: 0

oleksii
oleksii

Reputation: 35905

You would need to do this yourself. One of the possible solutions would be to use an extension method

public static class DoubleEx
{
    public static double TruncateFraction(this double value, int fractionRound)
    {
        double factor = Math.Pow(10, fractionRound);
        return Math.Truncate(value * factor) / factor;
    }
}

And this is how to use it

double foo = 50234.9489898997952932;
double bar = foo.TruncateFraction(4);

Console.WriteLine(foo); //50234.9489898997952932
Console.WriteLine(bar); //50234.9489

Upvotes: 2

Related Questions