空字符串双C# [英] Empty String to Double C#

查看:152
本文介绍了空字符串双C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这一刻,我试图从文本框中获得一个双重值:

At this moment i am trying to get a double value from textbox like this:

String.IsNullOrEmpty(textBox1.Text) ? 0.0 : Double.Parse(textBox1.Text)

但是有一个问题,我不能得到如何解析空文本框?

But there is a problem, i cant get how to parse empty textbox?

例如,如果要用OleDb和Excel与空文本框一起尝试此代码,我们将收到错误

For example if to try this code with OleDb and Excel with empty textbox, we will get error

System.FormatException:输入字符串格式不正确。

System.FormatException: Input string was not in a correct format.

推荐答案

double val;
if(!double.TryParse(textBox.Text,out val))
    val = 0.0

这篇关于空字符串双C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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