DecimalPrecisionConverter.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Data;
  8. namespace CyberX8_Themes.Converters
  9. {
  10. public class DecimalPrecisionConverter : IValueConverter
  11. {
  12. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  13. {
  14. if (value is double number && parameter is int precision)
  15. {
  16. return number.ToString($"F{precision}");
  17. }
  18. return value;
  19. }
  20. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  21. {
  22. if(value==null||parameter==null)
  23. {
  24. return Binding.DoNothing;
  25. }
  26. if (value is double number)
  27. {
  28. if (int.TryParse(parameter.ToString(), out int precision))
  29. {
  30. return Math.Round(number, precision);
  31. }
  32. else
  33. {
  34. return Binding.DoNothing;
  35. }
  36. }
  37. return Binding.DoNothing;
  38. }
  39. }
  40. }