将Asp.NET MVC Razor中的十进制格式化为#.###,##。

5

我目前正在开发一个ASP.NET MVC 4.5应用程序。

我尝试将模型中的属性呈现为正确的数字值:

ViewModel中的属性:

public decimal? Price { get; set; }

数据库中的数据如下所示:99999,99

我期望的格式应该是这样的:99.999,99

在我的Razor视图中,我像这样使用属性:

@Model.Price

很遗憾,它看起来仍然像这样:99999.99

你知道如何在视图中正确格式化该 十进制数? 值吗?

是否有不使用显示模板的解决方案?

谢谢!!!


这里是一个例子 https://dev59.com/mqPia4cB1Zd3GeqPxV2E#45059438 - Salah Akbari
它必须是一个文本框吗? - john.1020
1
你可以试试,在使用EditorFor时我认为你应该使用一个模板。 - Salah Akbari
1
@Model.Price.ToString("#.##") - Liam
2个回答

5

试试这个

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

显示

99.999,99

我尝试了以下所有值:

"C" 用于在您的文化中显示货币

"C2" 货币,小数点后两位

"C2",文化:我正在使用一个小数点为逗号的文化

"N2",文化:因为我只想显示数字,不要货币符号


嗨,谢谢!我现在在我的 Razor 视图中使用这个: - john.1020
@String.Format("{0:N0}", @Model.PreisVon) 请将上述代码翻译为中文。 - john.1020

1
请尝试使用本指南。
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));
     }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接