Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. /// <summary>
  2. /// Converts 14,159,431.12 or 14.159.431,12 into 14159431.12
  3. /// </summary>
  4. /// <param name="this"></param>
  5. /// <returns></returns>
  6. public static decimal FromPriceToDecimal(this string @this)
  7. {
  8. if (@this == null) return 0;
  9. NumberStyles style;
  10. CultureInfo provider;
  11. style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands;
  12. provider = new CultureInfo("en-US");
  13.  
  14. #region encontrar e remover pontos marcadores maiores que decimal
  15. Regex rx = new Regex(@"\.", RegexOptions.Compiled | RegexOptions.IgnoreCase);
  16. MatchCollection matches = rx.Matches(@this);
  17. if (matches.Count > 1)
  18. {
  19. for (int i = matches.Count; i > matches.Count - 1; i--)
  20. {
  21. @this = @this.Remove(matches[i - 1].Index, 1);
  22. }
  23. }
  24. #endregion
  25.  
  26. var formatedDecimal = Regex.Replace(@this, "[^0-9/.]", "");
  27. return decimal.Parse(formatedDecimal, style, provider);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement