Waller
Waller

Reputation: 476

How to display a var in string format?

In the view,
PAYMENT1 = 1,200.50
PAYMENT2 = 1,500.50

totalpayment = 2701

I want the totalpayment to have the format with comma.

totalpayment= 2,701.00

I tried using .ToString but didn't work. Any idea?

public ActionResult Edit(int id, bool update = false)
{
    TBL_PROJ project = db.TBL_PROJ.Find(id);

    ViewBag.PAYMENT1 = Convert.ToDecimal(project.PAYMENT1).ToString("#,##0.00");
    ViewBag.PAYMENT2 = Convert.ToDecimal(project.PAYMENT2).ToString("#,##0.00");

    var  totalPayment = project.PAYMENT1 + project.PAYMENT2;
    project.TotalPayment = (float)Math.Round(totalPayment, 2); //This Part

    return View(project);
}

Upvotes: 0

Views: 104

Answers (4)

the berserker
the berserker

Reputation: 1594

First of all, you are not dealing with "var", but an actual type (probably decimal or double), only that the type is implicitly defined (also inferred type). An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.

So what you are actually trying to do, is find a way how to display a float with formatting.

Since project.TotalPayment is a float and for quickly get it working, you could put it in a ViewBag, convert it to string with formatting and use it in your view template, as you do with the other two properties.

ViewBag.TotalPayment = Convert.ToDecimal(totalPayment).ToString("#,##0.00");

However, from the code I see I can expect that this is a generated property from your ORM and therefore your problem is that you can not modify your TBL_PROJ class. This is where the view model comes into the game and for your case could be defined like so:

public class EditPaymentViewModel 
{ 
    public EditPaymentViewModel(decimal p1, decimal p2)
    {
        this.Payment1 = p1;
        this.Payment2 = p2;
    }

    [DisplayFormat(DataFormatString = "{0:n2}")]
    public decimal Payment1  { get; set; }

    [DisplayFormat(DataFormatString = "{0:n2}")]
    public decimal Payment2  { get; set; }

    [DisplayFormat(DataFormatString = "{0:n2}")]
    public decimal TotalPayment  { get { return Payment1+Payment2; } }
}

Then your action method would look like this:

public ActionResult Edit(int id, bool update = false)
{
    TBL_PROJ project = db.TBL_PROJ.Find(id);

    var viewModel = new EditPaymentViewModel(project.Payment1, project.Payment2); 

    return View(project);
}

Of course, you would have to change your view to accept the model of type EditPaymentViewModel (@model EditPaymentViewModel). You can read more about DisplayFormatAttribute here.

Upvotes: 0

Pankaj Gupta
Pankaj Gupta

Reputation: 388

 var aa= 1200.60;
        var bb = 1245.50;
        var pay = Math.Round((aa + bb),2).ToString("#,##0.00");
 Console.WriteLine(pay);
result will be "2,446.10"

You have to apply format like that, hope it will help for you.

Upvotes: 0

The Shooter
The Shooter

Reputation: 733

Whereever you are displaying totalpayment use string.Format(format, totalpayment) or totalpayment.ToString(format);

As I expect totalPayment would become of type decimal and project.TotalPayment is also decimal type. Doing any formatting on totalpayment and storing it in project.TotalPayment makes no sense.

lets say you are displaying total payment in a label lblTotalPayment. You should do something like:

lblTotalPayment.Text = string.Format("{0:#,##0.00}", project.TotalPayment);

or

lblTotalPayment.Text = project.TotalPayment.ToString("#,##0.00");

Upvotes: 2

You have to apply format in last statement

project.TotalPayment = (float)Math.Round(totalPayment, 2).ToString("#,##0.00");

Upvotes: 0

Related Questions