将字符串转换或显示为货币

时间:2019-11-27 16:47:20

标签: c# asp.net-mvc linq

我正在使用C#和Linq,我打算用货币格式显示一系列添加到列表中的数据。

SQL Server中的数据(我要转换的是RealEjecutado <-)

100000.00

我希望这样显示它:

$100,000.00

我的代码

List<Dashboard> list = new List<Dashboard>();

using (Web_INCAEntities dc = new Web_INCAEntities())
{
        var v = (from a in dc.TBL_PBI
                 select new Dashboard
                 {
                     id = a.id,
                     RealEjecutado =  a.RealEjecutado,
                     PlanVigente =  a.PlanVigente,
                     Reprogramacion =  a.Reprogramacion
                 });
        list = v.ToList();
    }

    return View("../Dashboard/PontoHorizonte/Graficas", list);

标记:

@grid.GetHtml(
tableStyle: "fl-table",
htmlAttributes: new { id = "tablaadmin" },
    columns: grid.Columns(
                grid.Column(header: "Real Ejecutado", format: @<text><div class="" data-id="@item.id" data-propertyname="RealEjecutado" id="" ><p id="userinput">@item.RealEjecutado</p></div></text>),
                grid.Column(header: "Plan Vigente", format:@<text><div class="" data-id="@item.id" data-propertyname="PlanVigente">@item.PlanVigente</div></text>),
                grid.Column(header: "Proyección INCA", format:@<text><div class="" data-id="@item.id" data-propertyname="Reprogramacion">@item.Reprogramacion</div></text>)
                )
                )

在网络上我找不到对我有用的东西,这就是为什么请您帮忙解决这个问题,

3 个答案:

答案 0 :(得分:2)

以布兰登的答案为基础,您可以做到

i.ToString("C", CultureInfo.CreateSpecificCulture("en-US"))

这样获得美元格式

using (Web_INCAEntities dc = new Web_INCAEntities())
{
    var v = (from a in dc.TBL_PBI
             select new Dashboard
             {
                 id = a.id,
                 RealEjecutado =  a.RealEjecutado,
                 PlanVigente =  a.PlanVigente,
                 Reprogramacion =  a.Reprogramacion
             });
    list = v.ToList().Select(x => new Dashboard
             {
                 id = x.id,
                 RealEjecutado =  Decimal.TryParse(x.RealEjecutado, out decimal i) ? i.ToString("C", CultureInfo.CreateSpecificCulture("en-US")) : x.RealEjecutado,
                 PlanVigente =  x.PlanVigente,
                 Reprogramacion =  x.Reprogramacion
             }).ToList();     
}

return View("../Dashboard/PontoHorizonte/Graficas", list);

答案 1 :(得分:1)

这可能不是实现此目的的最有效方法,但是考虑到您在问题中所说的,它应该可以工作。第二个选择是因为我相信LinqToEntities将抱怨函数的用法。这将尝试将值解析为十进制。如果成功,它将使用货币字符串转换器。如果失败,它将仅使用RealEjecutado的裸值。

using (Web_INCAEntities dc = new Web_INCAEntities())
{
    var v = (from a in dc.TBL_PBI
             select new Dashboard
             {
                 id = a.id,
                 RealEjecutado =  a.RealEjecutado,
                 PlanVigente =  a.PlanVigente,
                 Reprogramacion =  a.Reprogramacion
             });
    list = v.ToList().Select(x => new Dashboard
             {
                 id = x.id,
                 RealEjecutado =  Decimal.TryParse(x.RealEjecutado, out decimal i) ? i.ToString("C") : x.RealEjecutado,
                 PlanVigente =  x.PlanVigente,
                 Reprogramacion =  x.Reprogramacion
             }).ToList();     
}

return View("../Dashboard/PontoHorizonte/Graficas", list);

答案 2 :(得分:0)

public static string DecimalToFormattedStringCurrency(decimal? decimalValue, string decimalFormatter = null)
        {
            if (String.IsNullOrWhiteSpace(decimalFormatter))
            {
                decimalFormatter = "{0:C0}";
            }

            return decimalValue.HasValue ? String.Format(decimalFormatter, decimalValue) : null;
        }