Last active
August 29, 2015 14:15
-
-
Save weiland/b7698548509e6b9c08cf to your computer and use it in GitHub Desktop.
C# Model: Dealing with Currency
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private string _articlePrice; | |
public string ArticlePrice { | |
get | |
{ | |
return _articlePrice; | |
} | |
set | |
{ | |
Thread.CurrentThread.CurrentCulture = new CultureInfo("de"); | |
_articlePrice = String.Format("{0:C}", decimal.Parse(value)); | |
} | |
} | |
// will output a nice 42,00€ instead of 42.00000 | |
// the CultureInfo can be changed to a different country and | |
// one can manually change the currency symbol |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Second approach which prevents converting errors | |
// Basically, you set the ArticlePriceDecimal which accepts a decimal value | |
// and you get the lang converted price as string via ArticlePrice | |
private string _articlePrice; | |
public decimal ArticlePriceDecimal | |
{ | |
set | |
{ | |
Thread.CurrentThread.CurrentCulture = new CultureInfo("de"); | |
_articlePrice = String.Format("{0:C}", value); | |
} | |
} | |
public string ArticlePrice { | |
get | |
{ | |
return _articlePrice; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment