How do I display a decimal value to 2 decimal places?

前端 未结 17 2147
没有蜡笔的小新
没有蜡笔的小新 2020-11-21 23:24

When displaying the value of a decimal currently with .ToString(), it\'s accurate to like 15 decimal places, and since I\'m using it to represent dollars and ce

相关标签:
17条回答
  • 2020-11-22 00:01
    Double Amount = 0;
    string amount;
    amount=string.Format("{0:F2}", Decimal.Parse(Amount.ToString()));
    
    0 讨论(0)
  • 2020-11-22 00:03
    decimalVar.ToString ("#.##"); // returns "" when decimalVar == 0
    

    or

    decimalVar.ToString ("0.##"); // returns "0"  when decimalVar == 0
    
    0 讨论(0)
  • If you need to keep only 2 decimal places (i.e. cut off all the rest of decimal digits):

    decimal val = 3.14789m;
    decimal result = Math.Floor(val * 100) / 100; // result = 3.14
    

    If you need to keep only 3 decimal places:

    decimal val = 3.14789m;
    decimal result = Math.Floor(val * 1000) / 1000; // result = 3.147
    
    0 讨论(0)
  • 2020-11-22 00:07

    I know this is an old question, but I was surprised to see that no one seemed to post an answer that;

    1. Didn't use bankers rounding
    2. Didn't keep the value as a decimal.

    This is what I would use:

    decimal.Round(yourValue, 2, MidpointRounding.AwayFromZero);
    

    http://msdn.microsoft.com/en-us/library/9s0xa85y.aspx

    0 讨论(0)
  • 2020-11-22 00:09

    Very rarely would you want an empty string if the value is 0.

    decimal test = 5.00;
    test.ToString("0.00");  //"5.00"
    decimal? test2 = 5.05;
    test2.ToString("0.00");  //"5.05"
    decimal? test3 = 0;
    test3.ToString("0.00");  //"0.00"
    

    The top rated answer is incorrect and has wasted 10 minutes of (most) people's time.

    0 讨论(0)
  • 2020-11-22 00:09

    The top-rated answer describes a method for formatting the string representation of the decimal value, and it works.

    However, if you actually want to change the precision saved to the actual value, you need to write something like the following:

    public static class PrecisionHelper
    {
        public static decimal TwoDecimalPlaces(this decimal value)
        {
            // These first lines eliminate all digits past two places.
            var timesHundred = (int) (value * 100);
            var removeZeroes = timesHundred / 100m;
    
            // In this implementation, I don't want to alter the underlying
            // value.  As such, if it needs greater precision to stay unaltered,
            // I return it.
            if (removeZeroes != value)
                return value;
    
            // Addition and subtraction can reliably change precision.  
            // For two decimal values A and B, (A + B) will have at least as 
            // many digits past the decimal point as A or B.
            return removeZeroes + 0.01m - 0.01m;
        }
    }
    

    An example unit test:

    [Test]
    public void PrecisionExampleUnitTest()
    {
        decimal a = 500m;
        decimal b = 99.99m;
        decimal c = 123.4m;
        decimal d = 10101.1000000m;
        decimal e = 908.7650m
    
        Assert.That(a.TwoDecimalPlaces().ToString(CultureInfo.InvariantCulture),
            Is.EqualTo("500.00"));
    
        Assert.That(b.TwoDecimalPlaces().ToString(CultureInfo.InvariantCulture),
            Is.EqualTo("99.99"));
    
        Assert.That(c.TwoDecimalPlaces().ToString(CultureInfo.InvariantCulture),
            Is.EqualTo("123.40"));
    
        Assert.That(d.TwoDecimalPlaces().ToString(CultureInfo.InvariantCulture),
            Is.EqualTo("10101.10"));
    
        // In this particular implementation, values that can't be expressed in
        // two decimal places are unaltered, so this remains as-is.
        Assert.That(e.TwoDecimalPlaces().ToString(CultureInfo.InvariantCulture),
            Is.EqualTo("908.7650"));
    }
    
    0 讨论(0)
提交回复
热议问题