Morquecho
Morquecho

Reputation: 13

Convert or display string to currency

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

Upvotes: 1

Views: 292

Answers (3)

user12447201
user12447201

Reputation:

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

Upvotes: 2

sam
sam

Reputation: 1985

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

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

Upvotes: 0

Brandon Barkley
Brandon Barkley

Reputation: 768

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

Upvotes: 1

Related Questions