我可以将默认的C#字符串覆盖为十进制转换器吗?

时间:2017-02-17 21:36:21

标签: c# wpf string decimal converter

在我的WPF程序中,有许多输入字段供用户指定金额。为了方便用户,我被要求修改字段,以便在用户输入10K时,一旦用户移动到下一个字段,该值就会更新为10,000。

其他一些捷径选择很少:

  • t,T,k,K ==>千
  • m,M ==>百万
  • b,B ==>十亿

如果只有一个字段,那么更改它会很容易,但由于多个屏幕上有很多字段,我正在寻找能够自动解析用户输入的简单解决方案。

那么可以将默认字符串覆盖为十进制转换器吗?

或者我可以将字符串的默认IFormatProvider更改为十进制转换吗?

1 个答案:

答案 0 :(得分:1)

您可以按照此示例操作。示例来自此处http://putridparrot.com/blog/typeconverters-and-xaml/

public class AbbreviatedNumberConverter : TypeConverter
    {
       public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
       {
          return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
       }

       public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
       {
          return destinationType == typeof(InstanceDescriptor) || 
                     base.CanConvertTo(context, destinationType);
       }

       public override object ConvertFrom(ITypeDescriptorContext context, 
                                CultureInfo culture, object value)
       {
          string text = value as string;
          if (text == null)
          {
             return base.ConvertFrom(context, culture, value);
          }

          if (String.IsNullOrWhiteSpace(text))
          {
             return 0.0;
          }

          if (culture == null)
          {
             culture = CultureInfo.CurrentCulture;
          }

          double number;
          if (AbbreviatedNumeric.ValidateDouble(text, out number, culture))
             return number;

          return 0.0;
       }

       public override object ConvertTo(ITypeDescriptorContext context, 
                         CultureInfo culture, object value, Type destinationType)
       {
          if (destinationType != null && value is Double)
          {
             if (destinationType == typeof(string))
             {
                return value.ToString();
         }
          }
          return base.ConvertTo(context, culture, value, destinationType);
       }
    }

    public Double Amount
    {
       get { return (Double)GetValue(AmountProperty); }
       set { SetValue(AmountProperty, value); }
    }

    public static readonly DependencyProperty AmountProperty =
                        DependencyProperty.Register("Amount", typeof(Double), 
                        typeof(OnlineStatusControl), new PropertyMetadata(0.0));


    [TypeConverter(typeof(AbbreviatedNumberConverter))]
    public Double Amount
    {
       get { return (Double)GetValue(AmountProperty); }
       set { SetValue(AmountProperty, value); }
    }

    public static class AbbreviatedNumeric
    {
       public static bool ValidateDouble(string value, out double? numeric, 
                  CultureInfo cultureInfo = null)
       {
          double result;
          if(ValidateDouble(value, out result, cultureInfo))
          {
             numeric = result;
             return true;
          }
          numeric = null;
          return false;
       }

       public static bool ValidateDouble(string value, out double numeric, 
                  CultureInfo cultureInfo = null)
       {    
          if (String.IsNullOrEmpty(value))
          {
             numeric = 0;
             return false;
          }

          if (Double.TryParse(value, out numeric))
          {
             return true;
          }
          if (value.Length > 0)
          {
             if (cultureInfo == null)
             {
            cultureInfo = CultureInfo.CurrentCulture;
         }

         NumberFormatInfo numberFormat = cultureInfo.NumberFormat;
         if (value.Substring(0, 1) == numberFormat.NumberDecimalSeparator)
         {
            value = "0" + value;
         }
         if (Double.TryParse(value.Substring(0, value.Length - 1), 
                         NumberStyles.AllowLeadingWhite | 
                         NumberStyles.AllowTrailingWhite |                      
                         NumberStyles.AllowLeadingSign |
                 NumberStyles.AllowDecimalPoint | 
                         NumberStyles.AllowThousands | 
                 NumberStyles.AllowExponent, cultureInfo, out numeric))
         {
            switch (Char.ToUpper(value[value.Length - 1]))
            {
                case 'B':
               numeric = numeric * 1000000000;
               break;
            case 'M':
               numeric = numeric * 1000000;
               break;
            case 'K':
               numeric = numeric * 1000;
               break;
            default:
               return false;
            }
                return true;
         }
          }
          return false;
       }
    }

您也可以通过

后面的C#代码执行此操作
static string FormatNumber(int num) {
    if (num >= 100000)
        return FormatNumber(num / 1000) + "K";

    return num.ToString("#,0");
}