using System; using System.Collections.Generic; using System.ComponentModel; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GeoSigma.SigmaDrawerStyle.Converter { public abstract class DoubleConverter : BaseTypeConverter { protected virtual string FmtString { get; set; } = "{0}"; public int DigitDecimal { get; set; } = -1; /// /// Initializes a new instance of the class. /// public DoubleConverter() : base() { } /// /// Initializes a new instance of the class. /// /// 小数位数 public DoubleConverter(int digitDecimal) : this() { this.DigitDecimal = digitDecimal; if (this.DigitDecimal >= 0) { FmtString = $"{{0:F{this.DigitDecimal}}}"; } } public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(string)) { return true; } return base.CanConvertFrom(context, sourceType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { return Convert.ToDouble(value); } return base.ConvertFrom(context, culture, value); } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(string)) { return true; } return base.CanConvertTo(context, destinationType); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string)) { return string.Format(FmtString, value); } return base.ConvertTo(context, culture, value, destinationType); } } public class Double0Converter : DoubleConverter { protected override string FmtString { get; set; } = "{0:F0}"; } public class Double1Converter : DoubleConverter { protected override string FmtString { get; set; } = "{0:F1}"; } public class Double8Converter : DoubleConverter { protected override string FmtString { get; set; } = "{0:0.########}"; } }