Jak sformatować właściwość dziesiętną na walutę?

Chcę sformatować wartość dziesiętną jako wartość waluty.

Jak mogę to zrobić?

Author: Alexander Abakumov, 2010-08-13

8 answers

Właściwości mogą zwracać wszystko, co chcą, ale będzie musiał zwrócić właściwy typ.

private decimal _amount;

public string FormattedAmount
{
    get { return string.Format("{0:C}", _amount); }
}
Zadano pytanie... a gdyby to był nullable dziesiętny.
private decimal? _amount;

public string FormattedAmount
{
    get
    {
         return _amount == null ? "null" : string.Format("{0:C}", _amount.Value);
    }
}  
 115
Author: Robaticus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-30 01:51:01

Poniżej również by zadziałało, ale nie można umieścić w getterze właściwości dziesiętnej. Getter właściwości decimal może zwrócić tylko wartość dziesiętną, dla której formatowanie nie ma zastosowania.

decimal moneyvalue = 1921.39m; 
string currencyValue = moneyvalue.ToString("C");
 35
Author: Russ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-13 17:36:53

Try this;

  string.Format(new CultureInfo("en-SG", false), "{0:c0}", 123423.083234);

Konwertuje 123423.083234 na format $1,23,423.

 12
Author: Mohan Gopi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-29 10:15:40

Możesz utworzyć metodę rozszerzenia. Uważam to za dobrą praktykę, ponieważ może być konieczne zablokowanie wyświetlacza waluty niezależnie od ustawień przeglądarki. Na przykład możesz wyświetlić $ 5,000. 00 zawsze zamiast 5 000,00 $ (#CanadaProblems)

public static class DecimalExtensions
{
    public static string ToCurrency(this decimal decimalValue)
    {
        return $"{decimalValue:C}";
    }
}
 8
Author: Atters,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-16 21:46:24

Możesz użyć String.Format, zobacz kod[via How-To Geek]:

decimal moneyvalue = 1921.39m;
string html = String.Format("Order Total: {0:C}", moneyvalue);
Console.WriteLine(html);
// Output: $1,921.39

Zobacz też:

 4
Author: Dariusz Woźniak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-13 17:34:27

Możesz teraz używać właściwości interpolacji łańcuchów i wyrażeń w C # 6.

private decimal _amount;

public string FormattedAmount => $"{_amount:C}";
 2
Author: Richie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-19 15:27:14

Twój zwracany format będzie ograniczony przez zadeklarowany Typ zwrotu. Tak więc tak, możesz zadeklarować właściwość jako łańcuch znaków i zwrócić sformatowaną wartość czegoś. W "get" możesz umieścić dowolny kod pobierania danych, którego potrzebujesz. Więc jeśli chcesz uzyskać dostęp do jakiejś wartości liczbowej, po prostu umieść instrukcję return jako:

    private decimal _myDecimalValue = 15.78m;
    public string MyFormattedValue
    {
        get { return _myDecimalValue.ToString("c"); }
        private set;  //makes this a 'read only' property.
    }
 1
Author: Austin Rhymer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-13 17:37:27

Typ dziesiętny nie może zawierać informacji o formatowaniu. Możesz utworzyć inną właściwość, powiedzmy FormattedProperty typu string, która robi to, co chcesz.

 0
Author: recursive,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-13 17:31:19