Convert.ToDouble异常:输入字符串的格式不正确 [英] Convert.ToDouble exception: Input string was not in a correct format

查看:165
本文介绍了Convert.ToDouble异常:输入字符串的格式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将1.1之类的数字转换为双精度数时,会抛出异常:输入字符串的格式不正确."我不知道为什么会这样,因为我提供的输入是正确的,而我要转换为的变量是双精度型.下面是代码:

When i try to convert a number like 1.1 to a double i get thrown a exception: "Input string was not in a correct format." I don't know why this is happening since the input i give is correct and the variable where i am converting to is a double. Below is the code:

控制器创建方法:

        [ValidateAntiForgeryToken]
        public ActionResult Create(IFormCollection collection)
        {
            try
            {
                Riskanalysis riskanalysis = new Riskanalysis()
                {
                    dateCreation = Convert.ToDateTime(collection["dateCreation"]),
                    userId = collection["userId"],
                    categoryId = Convert.ToInt32(collection["categoryId"]),
                    zoneId = Convert.ToInt32(collection["zoneId"]),
                    equipmentId = Convert.ToInt32(collection["equipmentId"]),
                    taskId = Convert.ToInt32(collection["taskId"]),
                    activity = collection["activity"],
                    action = collection["action"],
                    descriptionPotentialRisk = collection["descriptionPotentialRisk"],
                    dangerId = Convert.ToInt32(collection["dangerId"]),
                    consequenceId = Convert.ToInt32(collection["consequenceId"]),
                    dangerousEnergiesId = Convert.ToInt32(collection["dangerousEnergiesId"]),
                    preventionMeasures = collection["preventionMeasures"],
                    safetyProcedures = collection["safetyProcedures"],
                    preSeriousness = Convert.ToDouble(collection["preSeriousness"]),
                    preProbability = Convert.ToDouble(collection["preProbability"]),
                    preExposure = Convert.ToDouble(collection["preExposure"]),
                    preRiskDegree = Convert.ToDouble(collection["preRiskDegree"]),
                    preRiskCategory = Convert.ToDouble(collection["preRiskCategory"]),
                };

                DanoneRiskanalysisManager rm = new DanoneRiskanalysisManager();
                rm.InsertRiskanalysis(riskanalysis);

                return RedirectToAction(nameof(Create));
            }
            catch (Exception e)
            {
                return View();
            }
        } 

输入字段:

                <div class="form-group">
                    <label asp-for="preSeriousness" class="control-label">Pre seriousness:</label>
                    <input asp-for="preSeriousness" class="form-control" type="text" required/>
                    <span asp-validation-for="preSeriousness" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="preProbability" class="control-label">Pre probability:</label>
                    <input asp-for="preProbability" class="form-control" type="text" required/>
                    <span asp-validation-for="preProbability" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="preExposure" class="control-label">Pre exposure:</label>
                    <input asp-for="preExposure" class="form-control" type="text" required/>
                    <span asp-validation-for="preExposure" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="preRiskDegree" class="control-label">Pre risk degree:</label>
                    <input asp-for="preRiskDegree" class="form-control" type="text" required/>
                    <span asp-validation-for="preRiskDegree" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="preRiskCategory" class="control-label">Pre risk category:</label>
                    <input asp-for="preRiskCategory" class="form-control" type="text" required/>
                    <span asp-validation-for="preRiskCategory" class="text-danger"></span>
                </div>

有人知道原因可能是什么,或在哪里寻找?

Anyone knows what the cause could be or where to look for?

已经感谢!

推荐答案

您是否检查过语言环境?,可能数字格式未使用".作为小数点分隔符,请尝试键入,"而不是.".

have you checked locale yet ?, probably the number format doesn't use "." As a decimal separator, try typing "," instead of "."

有关更多信息,请参见:

for more information see: support.microsoft

这篇关于Convert.ToDouble异常:输入字符串的格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆