Ancient
Ancient

Reputation: 3057

Format decimal property to currency inside class

I have created a class

public class XYZ: DomainObject
{   
    public decimal Price { get; set; }
    public decimal Commission { get; set; }
}

Now I use Price property in many place . I get a requirement to change my price value 5454 to 5,454.00 .

So I use this one

@String.Format("{0:N}", Model.Price)

But in this approach I have to do above thing at so many places I want to do something with my Price property inside my class . So that when ever a person try to get this property . He gets the formatted value 5,454.00 .

What should I do ?

Upvotes: 0

Views: 1791

Answers (2)

Dimitar Dimitrov
Dimitar Dimitrov

Reputation: 15158

Well what about something like this:

public string PriceFormatted { get { return string.Format("{0:N}", Price); } }

Upvotes: 0

MarcinJuraszek
MarcinJuraszek

Reputation: 125650

Your property is a decimal, so it has no format assigned to it. You can add another property of string and hide the format there:

public class XYZ: DomainObject
{   
    public decimal Price { get; set; }

    public string PriceString
    {
        get { return Price.ToString("N"); }
    }

    public decimal Commission { get; set; }
}

Upvotes: 6

Related Questions