1

I am working with C# and Linq and what I intend is to show a series of data that I add to a list with the currency format.

Data in SQL Server (RealEjecutado <-- is what i want to convert)

100000.00

I want it to be displayed like this:

$100,000.00

My code

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);

Markup:

@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>)
                )
                )

I have not found on the web something that works for me, that is why I ask your help to solve this, thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Morquecho
  • 13
  • 3
  • what property in Dashboard represents currency? – d.moncada Nov 27 '19 at 16:50
  • RealEjecutado <-- is what i want to convert – Morquecho Nov 27 '19 at 16:51
  • 2
    `.ToString("C")` see https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings Assuming that the data is numeric. If it's stored in the DB as a string you can parse to a numeric value first. – juharr Nov 27 '19 at 16:51
  • Does this answer your question? [ASP.NET MVC data annotation for currency format](https://stackoverflow.com/questions/29975128/asp-net-mvc-data-annotation-for-currency-format) – gandalf Nov 27 '19 at 16:53
  • @Morquecho what type is it, a string? – d.moncada Nov 27 '19 at 16:53
  • It tells me the following: it cannot be converted from 'string' to 'System.IFormatProvider' ..... I guess since SQL must be int and not varchar so it can be converted? – Morquecho Nov 27 '19 at 16:55
  • @Morquecho Nope. That is the Compiler telling you "dude, that code you wrote makes no sesnse". The SQL Server? You never even got close to calling it. – Christopher Nov 27 '19 at 17:00
  • You need to specifry what is happening or not happening with your code. Right now your problem is unclear. – Christopher Nov 27 '19 at 17:02
  • Check the following: No overload for the 'ToString' method takes 1 arguments – Morquecho Nov 27 '19 at 17:05

3 Answers3

2

Building off of Brandon's answer, you can do

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

to get the dollar format like so

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

This is possibly not the most efficient way to accomplish this, but it should work given what you said in the question. The second select is because I believe LinqToEntities will complain about the function usages. This will try to parse the value to Decimal. If successful, it will then use the currency string converter. If it fails, it will just use the bare value of 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);
Brandon Barkley
  • 720
  • 6
  • 21
  • if it worked, only if I still specify the .Tosting ("C") format I get as a result -> 865,873.00 € and what I'm looking for is $865.873.00 – Morquecho Nov 27 '19 at 17:44
  • That would be an issue related to your current culture. There should be another ToString overload where you can pass in the appropriate culture. – Brandon Barkley Dec 11 '19 at 20:21
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;
        }
sam
  • 1,937
  • 1
  • 8
  • 14