1

I have Article model which has property SellPrice. I want wherever I use it to be displayed with 2 numbers after the decimal separator. It always has value of 2 numbers after the decimal separator but when the price is for example 2,30 it's displayed as 2,3 and I want to be displayed as 2,30. The same thing happens for property Quantity in the same Article model I want it to be displayed with 3 numbers after the decimal separator for example if its value is 1,1 to be displayed as 1,100. For SellPrice I tried the following:

[Column("sell_price")]
[XmlElement(ElementName = "sell_price", Namespace = "http://tempuri.org/DataSet1.xsd")]
[DisplayFormat(DataFormatString = "{0:C}")]
public decimal SellPrice { get; set; }

But DisplayFormat is underlined with red and I'm not allowed to import its namespace using System.ComponentModel.DataAnnotations. I guess it's deprecated. For displaying 3 numbers after decimal separator I didn't find even something deprecated. I found a lot of ways to do it using String.Format but I use SellPrice and Quantity in a lot of places in my project and I don't want every time when I use the model properties to write String.Format...... Is there any way to specify that in the model as attribute for example?

York Shen
  • 9,014
  • 1
  • 16
  • 40

3 Answers3

5

Why not use a private field to save the value in and have two properties SellPrice and SellPriceString as followed, this way you can re-use the SellPriceString property instead of formatting the string each time you want to use the SellPrice property:

decimal _sellPrice;
public decimal SellPrice
{
    get
    {
        return _sellPrice;
    }
    set
    {
        _sellPrice = value;
    }
}

public string SellPriceString
{
    get
    {
        return _sellPrice.ToString("N2");
    }
}

Using a Standard Numeric Format as parameter in your ToString method. You would do exactly the same with your Quantity property, but using the Standard Numeric Format "N3", once again refer to the link for more information on the format.

Raimo
  • 1,494
  • 1
  • 10
  • 19
  • Is it not possible to do it using `[DisplayFormat]` attribute as in MVC? – Profile3ForStack Feb 08 '18 at 11:27
  • @Profile3ForStack Were you able to import the namespace already? If not, you might want to have a look at this topic on SO, more specifically this answer: https://stackoverflow.com/a/10174457/7016022. I do not work with MVC while developing a Xamarin.Android app, I use MVVMCross. – Raimo Feb 08 '18 at 14:03
  • @Profile3ForStack If this solved your problem, please accept my answer. If not, can you give us an update? – Raimo Mar 08 '18 at 13:46
  • "C2" makes currency to display too. I don't want that. I pass bellow my solution which has part of the suggestions in that post and part of my way to do it. I think it responds the best way to the question. I gave you +1 because part of your solution lead me to my solution. – Profile3ForStack Mar 08 '18 at 14:27
  • @Profile3ForStack You could just use "N2" then.. That's why I referred to this documention: https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings, I don't see any reason why you wouldn't accept my answer. I edited my answer to reflect what I mean. – Raimo Mar 08 '18 at 14:30
0

How to display 2 or 3 numbers after the decimal separator in Xamarin.Android?

You could try using SellPrice.ToString("C2"), refer to: Standard Numeric Format Strings.

For example:

SellPrice = 12.545646M;
Debug.WriteLine("SellPrice.ToString(C2) == " + SellPrice.ToString("C2"));

[0:] SellPrice.ToString(C2) == ¥12.55
York Shen
  • 9,014
  • 1
  • 16
  • 40
  • But I want to set it in one place(in the model for example) and then wherever I use it to be displayed always with 2 numbers after the decimal separator even when the number is 7,6 for example , not everytime I have to use SellPrice to have to put some formating code. – Profile3ForStack Feb 08 '18 at 07:11
  • 1
    Then create a model property 'string SellPriceDisplay' and include the above code in the 'get' part. – Nick Kovalsky Feb 08 '18 at 11:14
0
public decimal Quantity { get; set; }
public string QuantityDisplay
{
    get
    {
        return String.Format("{0:0.000}", Quantity);
    }
}
[Column("sell_price")]
[XmlElement(ElementName = "sell_price", Namespace = "http://tempuri.org/DataSet1.xsd")]
//[DisplayFormat(DataFormatString = "{0:0.00}")]
public decimal SellPrice { get; set; }
public string SellPriceDisplay
{
    get
    {
        return String.Format("{0:0.00}", SellPrice);
    }
}

Wherever I want to make calculations I use Quantity and SellPrice properties. Wherever I want to display Quantity and SellPrice I use QuantityDisplay and SellPriceDisplay properties.