ASP.Net核心货币格式无法正常工作

时间:2017-09-11 21:51:23

标签: c# asp.net .net asp.net-mvc asp.net-core

尝试使用属性3 times显示货币并使用[DataType(DataType.Currency)]时,在本地调试时,我的OS X计算机上的格式无法正确显示。它显示为¤符号而不是$。

2 个答案:

答案 0 :(得分:3)

您可以在Startup.cs

中以编程方式为服务器设置区域性信息
public Startup(IHostingEnvironment env)
{
    CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");

    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}

答案 1 :(得分:0)

这是另一种方法:

型号:

namespace WebApplication1.Models
{
    public class Employee
    {
        public decimal Salary { get; set; }
        public string SalaryUS
        {
            get { return string.Format(new System.Globalization.CultureInfo("en-US"), "{0:C}", Salary); }
        }
        public string SalaryPL
        {
            get { return string.Format(new System.Globalization.CultureInfo("pl-PL"), "{0:C}", Salary); }
        }
    }
}

控制器:

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            var model = new Employee()
            {
                Salary = 1000
            };

            return View(model);
        }
    }
}

查看:

@model WebApplication1.Models.Employee

@Html.DisplayFor(model => model.SalaryUS) <br/>
@Html.DisplayFor(model => model.SalaryPL)

结果:

$1,000.00 
1 000,00 zł