Format a decimal in a view

后端 未结 3 506
暗喜
暗喜 2021-01-17 10:05

If I have a decimal value in a field, and I am trying to display on a page, how do I format it (I would use string.format in web forms):

@Html.DisplayFor(Fun         


        
相关标签:
3条回答
  • 2021-01-17 10:17

    dont do in DisplayFor... in the class set the atributte like this one.

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:N2}")]
        public virtual decimal Valor
        {
            get;
            set;
        }
    
    0 讨论(0)
  • 2021-01-17 10:28

    I wouldn't do this inside the view. It would be better to centralize this and provide this information as metadata as below:

    public class Foo { 
    
        [DisplayFormat(DataFormatString = "{0:n0}")]
        public decimal Bar { get; set; }
    }
    

    Then use this as usual:

    @Html.DisplayFor(m => m.Bar)
    
    0 讨论(0)
  • 2021-01-17 10:29

    Do you need to use Html.DisplayFor?

    Otherwise you can just do:

    @String.Format("{0:n0}",currentItem.QualityM1)
    
    0 讨论(0)
提交回复
热议问题