Guest User

Untitled

a guest
Sep 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. string to Currency format, with no periods (or commas)
  2. value = Convert.ToDecimal(value).ToString("C2");
  3.  
  4. var stringValue = Convert.ToDecimal(value).ToString("$0.00");
  5.  
  6. var formatInfo = (NumberFormatInfo)CultureInfo.CurrentCulture.NumberFormat.Clone();
  7. formatInfo.CurrencyGroupSeparator = string.Empty;
  8.  
  9. var stringValue = Convert.ToDecimal(value).ToString("C", formatInfo);
  10.  
  11. var formatInfo = (System.Globalization.NumberFormatInfo)CultureInfo.CurrentCulture.NumberFormat.Clone();
  12. formatInfo.CurrencyGroupSeparator = ""; // remove the group separator
  13. Console.WriteLine(2.ToString("C", formatInfo));
  14. Console.WriteLine(4.ToString("C", formatInfo));
  15. Console.WriteLine(1000.ToString("C", formatInfo));
  16.  
  17. public static class MyExtensions
  18. {
  19. public static string GetMoney(this decimal value, bool displayCurrency = false, bool displayPeriods = true)
  20. {
  21. string ret = string.Format("{0:C}", value).Substring(displayCurrency ? 0 : 1);
  22. if (!displayPeriods)
  23. {
  24. ret = ret.Replace(",", string.Empty);
  25. }
  26. return ret;
  27. }
  28. }
  29.  
  30. decimal test = 40023.2345M;
  31. string myValue = test.GetMoney(displayCurrency:true, displayPeriods:false);`
Add Comment
Please, Sign In to add comment