本文介绍了双.parse失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 var amt = "2.8-";double reqamt = Double.Parse(climateString.Replace(",", "")); MessageBox.Show(reqamt.ToString()); 我在下面例外: I am getting below exception: "Input string was not in a correct format." 我尝试了什么: 当var amt = - 2.8时,这个工作正常; 当var amt =2.8-时,我能否正常工作; 因为数据库向我发送结果像2.8 - ; What I have tried:This is working fine when var amt = "-2.8";Can I get this working when var amt = "2.8-";Because Database sends me results like "2.8-";推荐答案 using System;using System.Globalization;namespace TryParse_Sandbox{ class Program { static void Main(string[] args) { string[] input = { "2.8", "-2.8", "2.8-", "0", "-2", "2-" }; NumberStyles style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingSign | NumberStyles.AllowThousands; CultureInfo culture = CultureInfo.CurrentUICulture; foreach (var number in input) { double value; if (double.TryParse(number, style, culture, out value)) Console.WriteLine( 这篇关于双.parse失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-03 09:05