MVC định dạng tiền tệ vnđ - Format currency vnd in MVC

Hi, Xin chào,
I want to format currency vnd(vietnamese) in MVC and it is working for me.
Step 1: In model
public class MyCurrency


     [Display(Name = "Giá")]
     public decimal? Price { get; set; }     
}

Step 2: In View

@model Demo.Web.Models.CurrencyModel
@{    
    // define format VND
    var format = System.Globalization.CultureInfo.GetCultureInfo("vi-VN");
    var currency = 250000 //example

}

<div>
      @String.Format(format, "{0:c0}", currency );
       //hoặc nếu các bạn lấy dữ liệu từ model thì thay value bằng biến model, 
       //ví dụ mình thay value bằng @item.Price
       //@String.Format(format, "{0:c0}", @item.Price );
</div>

Và đây là kết quả của tôi:
Trong admin tôi nhập: 1050000


Và hiển thị kết quả:

Mình làm demo đơn giản để các bạn có thêm nguồn tài liệu tham khảo. Bạn có thể xem thêm các định dạng khác tại Custom numeric format strings hoặc Standard numeric format strings

Nhận xét

Bài đăng phổ biến từ blog này

Khôi phục phân vùng ổ cứng do ghost nhầm theo Hieuit.net

Cách sử dụng 2 phương thức [httppost] và [httpget] trong MVC theo https://dzungdt.wordpress.com