转换为 double 不允许小数点后有零

Converting to double is not allowing a zero after the decimal place

我在 WPF 和 C# 中工作,运行 遇到一个我认为源于我的转换器的问题。我有一个文本框,其中在页面加载时将 INT 转换为 DOUBLE 并显示。 示例:

我输入 24.0 后会发生什么,它会立即恢复为 24。我可以通过输入 24.9 然后在我需要的地方输入 0 来实现 24.09。我试图弄乱我的转换器,认为这是 when/how 的问题我正在将它转换为双精度,但它仍然产生相同的结果。

这是转换器的代码:

    //This takes in an int and converts it to a double with two decimal places
    [ValueConversion(typeof(object), typeof(double))]
    public class conDouble : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double db = System.Convert.ToDouble(value);
            return (db / 100.00).ToString();
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return System.Convert.ToInt32((System.Convert.ToDouble(value) * 100));
        }
    }

有问题的正则表达式文本框:

<Page.Resources>
        <system:String x:Key="regexDouble">^\d+(\.\d{1,2})?$</system:String>            
</Page.Resources>
<TextBox Name="txtItemPrice" Grid.Column="12" Grid.ColumnSpan="2" Grid.Row="4" HorizontalAlignment="Stretch" VerticalAlignment="Center" IsEnabled="False" 
                 Validation.ErrorTemplate="{StaticResource validationTemplate}"
                 Style="{StaticResource textStyleTextBox}">
            <TextBox.Text>
                <Binding Path="intPrice" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" Converter="{StaticResource Double}">
                    <Binding.ValidationRules>
                        <classobjects:RegexValidation Expression="{StaticResource regexDouble}"/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>

最后是我的验证者:

public class RegexValidation : ValidationRule
    {
        private string pattern;
        private Regex regex;
        public string Expression
        {
            get { return pattern; }
            set
            {
                pattern = value;
                regex = new Regex(pattern, RegexOptions.IgnoreCase);
            }
        }
        public RegexValidation() { }
        public override ValidationResult Validate(object value, CultureInfo ultureInfo)
        {
            if (value == null || !regex.IsMatch(value.ToString()))
            {
                return new ValidationResult(false, "Illegal Characters");
            }
            else
            {
                return new ValidationResult(true, null);
            }
        }

    }

在您的转换器中,行 return (db / 100.00).ToString(); 将双精度转换为字符串,正如 John Wu 提到的那样。作为测试,如果您 运行 表达式 (24.0).ToString(),结果为“24”。由于您正在为价格建模,您可以执行类似 (db / 100.00).ToString("F2") 的操作,这将导致“24.00”,或 (db / 100.00).ToString("C"),这将导致 $24.00。您可以在 https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings 找到格式字符串的完整列表。我没有完全遵循您的实施,但这可以防止 24.0 自动恢复为 24 的情况。

另外,请注意,十进制是比双精度更好的金融数据类型。有关更多信息,请参阅此 link:decimal vs double! - Which one should I use and when?