Truncate number of digit of double value in C#

后端 未结 12 684
死守一世寂寞
死守一世寂寞 2020-12-30 04:35

How can i truncate the leading digit of double value in C#,I have tried Math.Round(doublevalue,2) but not giving the require result. and i didn\'t find any other method in M

相关标签:
12条回答
  • 2020-12-30 04:38

    This code....

    double x = 12.123456789;
    Console.WriteLine(x);
    x = Math.Round(x, 2);
    Console.WriteLine(x);
    

    Returns this....

    12.123456789
    12.12
    

    What is your desired result that is different?

    If you want to keep the value as a double, and just strip of any digits after the second decimal place and not actually round the number then you can simply subtract 0.005 from your number so that round will then work. For example.

    double x = 98.7654321;
    Console.WriteLine(x);
    double y = Math.Round(x - 0.005, 2);
    Console.WriteLine(y);
    

    Produces this...

    98.7654321
    98.76
    
    0 讨论(0)
  • 2020-12-30 04:39

    There are a lot of answers using Math.Truncate(double). However, the approach using Math.Truncate(double) can lead to incorrect results. For instance, it will return 5.01 truncating 5.02, because multiplying of double values doesn't work precisely and 5.02*100=501.99999999999994

    If you really need this precision, consider, converting to Decimal before truncating.

    public static double Truncate(double value, int precision)
    {
        decimal power = (decimal)Math.Pow(10, precision);
        return (double)(Math.Truncate((decimal)value * power) / power);
    }
    

    Still, this approach is ~10 times slower.

    0 讨论(0)
  • 2020-12-30 04:41

    EDIT: It's been pointed out that these approaches round the value instead of truncating. It's hard to genuinely truncate a double value because it's not really in the right base... but truncating a decimal value is more feasible.

    You should use an appropriate format string, either custom or standard, e.g.

    string x = d.ToString("0.00");
    

    or

    string x = d.ToString("F2");
    

    It's worth being aware that a double value itself doesn't "know" how many decimal places it has. It's only when you convert it to a string that it really makes sense to do so. Using Math.Round will get the closest double value to x.xx00000 (if you see what I mean) but it almost certainly won't be the exact value x.xx00000 due to the way binary floating point types work.

    If you need this for anything other than string formatting, you should consider using decimal instead. What does the value actually represent?

    I have articles on binary floating point and decimal floating point in .NET which you may find useful.

    0 讨论(0)
  • 2020-12-30 04:42

    This could work (although not tested):

    public double RoundDown(this double value, int digits)
    {
         int factor = Math.Pow(10,digits);
    
         return Math.Truncate(value * factor) / factor;
    }
    

    Then you simply use it like this:

    double rounded = number.RoundDown(2);
    
    0 讨论(0)
  • 2020-12-30 04:46

    This maybe for decimal? I wrote my own coz of struggling to find answers online. This code is probably not fullproof as I lack the experience to fullproof it. Will eventually rebuild this method into something simple and awesome to get the job done in fewer steps with overall robustness improved too. You could easily convert it to take an input for spacing count. but I can not guarentee that it will function properly with other than two dec. Does not like plain inputs.. It seems to like a few 000.000 to work with as I am processing weight with 0000.000's coming through. Also, rounding prices with this after being calculated.

    private decimal TruncateToTwoDecimals(string value)
            {
    
                int error_ReturnZeroIfThisIsOne = 0;
                int tempArrayLength = value.Length;
                char[] tempArray = new char[tempArrayLength];
    
                int startCount = 0; //starts the counter when it hits punctuation
                int newCounter = 0; //counts twice to ensure only two decimal places after the comma/period
                int nextArraySize = 1; //gets the size spec ready for the return array/next array (needs to be + 1)
    
                int tempArrayCurrentIndex = 0;
                foreach (Char thisChar in value)
                {
                    tempArray[tempArrayCurrentIndex] = thisChar;
                    tempArrayCurrentIndex++;
                }
    
    
                for (int i = 0; i < tempArrayLength; i++)
                {
    
                    if (tempArray[i].ToString() == "," || tempArray[i].ToString() == ".")
                    {
                        startCount = 1;
                    }
                    if (startCount == 1)
                    {
                        newCounter++;
                    }
    
    
                    if (newCounter > 2)
                    {
                        break;
                    }
                    nextArraySize++;
                }
    
                char[] returnArray = new char[nextArraySize];
                int tempErrorCheckBelow = 0;
                for (int i = 0; i < nextArraySize; i++)
                {
    
    
                    try
                    {
                        returnArray[i] = tempArray[i]; //left array will read [i] till the spot in right array where second decimal and ignore the rest. snip snip
                    }
                    catch (Exception)
                    {
                        if (tempErrorCheckBelow == 2)
                        {
                            MessageBox.Show("OutOfBounds", "Error_ArrayIndex Out Of Bounds @TTTD");
                        }
                        returnArray[i] = Convert.ToChar("0");
                        tempErrorCheckBelow++;
                    }
    
                }
    
                if (tempArrayLength < 1)
                {
    
                    error_ReturnZeroIfThisIsOne++;
    
                    string tempString = "0.0";
                    try
                    {
                        decimal tempDecimal = Decimal.Parse(tempString);
                        return tempDecimal;
                    }
                    catch (Exception)
                    {
    
                        MessageBox.Show("Error_input String received Incorrect Format @TTTD");
                        return 0.00m;
                    }
                }
                else
                {
    
                    error_ReturnZeroIfThisIsOne++;
    
                    string tempString = " ";
                    tempString = new string(returnArray);
                    try
                    {
                        decimal tempDecimal = Decimal.Parse(tempString);
                        return tempDecimal;
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Error_inputString _0_ @TTTD");
                        return 0m;
    
                    }
                }
    
    
    
            }
    
    0 讨论(0)
  • 2020-12-30 04:47
    object number = 12.123345534;   
    string.Format({"0:00"},number.ToString());
    
    0 讨论(0)
提交回复
热议问题