How can I format decimal property to currency?

前端 未结 8 1499
眼角桃花
眼角桃花 2020-12-02 21:50

I want to format a decimal value as a currency value.

How can I do this?

8条回答
  •  不思量自难忘°
    2020-12-02 22:23

    Your returned format will be limited by the return type you declare. So yes, you can declare the property as a string and return the formatted value of something. In the "get" you can put whatever data retrieval code you need. So if you need to access some numeric value, simply put your return statement as:

        private decimal _myDecimalValue = 15.78m;
        public string MyFormattedValue
        {
            get { return _myDecimalValue.ToString("c"); }
            private set;  //makes this a 'read only' property.
        }
    

提交回复
热议问题