简体   繁体   中英

Displaying decimal values retrieved from Sql Server Database with thousands seperator on an Asp.net/Mvc Razor view

I am working on an ASP.NET/MVC application and I fall into this issue. I am using decimal data type to store my data in the Microsoft SQL Server Database, say fieldname is Volume .
I want to retrieve this in Razor view say using @model.Volume . It is displaying quite alright however I want to display data in this format 1,000000.00 , 1,000.00 not the normal 1000000 .

Model Class:

public class Quantity {
  public decimal Volume { get; set; }
}

View:

@model.Volume

In your model add the following data annotation to provide metadata to your view. This helps keep the logic for your model centralised.

[DisplayFormat(DataFormatString = "{0:n}")]
public decimal Volume { get; set; }

您可以在视图中使用ToString()格式设置功能:

@Model.Volume.ToString("#,##0.00")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM