简体   繁体   English

Asp.NET MVC剃刀格式十进制? 至 #。###,##

[英]Asp.NET MVC Razor Format Decimal? to #.###,##

I'm currently working on an ASP.NET MVC 4.5 application. 我目前正在研究ASP.NET MVC 4.5应用程序。

I try to render a property from my model as a correct numeric value: 我尝试将模型中的属性呈现为正确的数值:

The property in the ViewModel: ViewModel中的属性:

public decimal? Price { get; set; }

the data from the DB looks like this: 99999,99 来自数据库的数据如下所示: 99999,99

My desired format would look like this: 99.999,99 我想要的格式如下: 99.999,99

In my razor View I use the property like this: 在我的剃刀视图中,我使用如下属性:

@Model.Price

Unfortunately it looks still like this: 99999.99 不幸的是,它看起来仍然像这样: 99999.99

Do you know how I can format that decimal? 您知道如何格式化该小数吗? value correctly on my view? 在我看来正确值吗?

Is there also a solution without using a display template? 是否有不使用显示模板的解决方案?

Thanks!!! 谢谢!!!

Try this 尝试这个

decimal value = 99999.99M;
string display = value.ToString("N2", CultureInfo.GetCultureInfo("es"));

Displays 展示架

99.999,99 99.999,99

I have tried all the values below: 我已经尝试了以下所有值:

"C" is used for displaying currency in your culture "C"用于在您的文化中显示货币

"C2" currency with two digits after decimal point 小数点后两位数字的"C2"货币

"C2", culture : I'm using a culture which decimal point is comma "C2", culture :我使用的小数点是逗号的文化

"N2", culture : Because I want to show only number, without currency symbol "N2", culture :因为我只想显示数字,没有货币符号

Please Try Using This Guide 请尝试使用本指南

using System;
using System.Globalization;

public class TestClass
{
     public static void Main()
     {
         int i = 100;

         // Creates a CultureInfo for English in Belize.
         CultureInfo bz = new CultureInfo("en-BZ");
         // Displays i formatted as currency for the bz.
         Console.WriteLine(i.ToString("c", bz));

         // Creates a CultureInfo for English in the U.S.
         CultureInfo us = new CultureInfo("en-US");
         // Display i formatted as currency for us.
         Console.WriteLine(i.ToString("c", us));

         // Creates a CultureInfo for Danish in Denmark.
         CultureInfo dk = new CultureInfo("da-DK");
         // Displays i formatted as currency for dk.
         Console.WriteLine(i.ToString("c", dk));
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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