1

decimal(8,4)でdecimalプロパティを宣言したい。ただし、C#のデフォルトはdecimal(18,2)です。

    public class GroupItems
    {

        public GroupItems()
        {


        }

        public string ItemCode { get; set; }
        public string ItemDesc { get; set; }
        public decimal ItemPrice { get; set; }
        public decimal ItemAmount { get; set; }
        public double ItemQty { get; set; }


    }
4

3 に答える 3

2

それを直接行うことはできません。set メソッドを使用して、この動作を実装できます。

    private decimal _myProperty;

    public decimal MyProperty
    {
        get { return _myProperty; }
        set {
            if (value <= 99999999) //for 8 
            {
                _myProperty = Math.Round(value,4);
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
    }
于 2012-05-25T06:54:00.100 に答える
1

あなたが達成したいことのために、あなたはこのようなことをすることができます

  decimal _ItemPrice; 
  public decimal ItemPrice 
  { get
    { return Math.Round(_ItemPrice, 2) } 
    set
    { _ItemPrice = value;}
  } 

上記のコードのように、プロパティの get と set を変更する必要があります。

于 2012-05-25T06:53:48.820 に答える
-1

.net 10 進数の精度は「有効桁数 28 ~ 29 桁」で、この場合には十分です。リンク

于 2012-05-25T06:55:55.293 に答える