如何传递空十进制值? [英] How to pass empty decimal value?
本文介绍了如何传递空十进制值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用下面两个用于将空值分配给字符串并传递空字符串值。
string test =;
string test1 = string.Empty;
但我使用十进制为空
I have use below two for Allocate empty value to string and pass the empty string value.
string test = "";
string test1 = string.Empty;
But i use decimal as empty
decimal test3 = "";
this is show
Cannot Implicitly convert type 'string' to 'decimal'
我是什么尝试过:
尝试另一个在线推荐使用来修复这个
What I have tried:
try another to refer in online to use for fix this
string test1 = string.Empty;
decimal test2 = decimal.Parse(test1);
but it shows
Input string was not in a correct format.
推荐答案
您无法将字符串
值分配给十进制
类型变量
使用
You cannot assign astring
value todecimal
type variable
use
decimal a= default(decimal);
// or
decimal a1 = 0;
您不能将小数设置为空 - 它是一个值只包含数值的类型。
有两种基本方法可以解决这个问题:
1)将其设置为无用值 - 可以是零,十进制。 MinValue或decimal.MaxValue。然后你可以测试它是否是那个特定值,并在必要时忽略它。
2)使用可以为空的小数:
You can't set a decimal to "empty" - it is a value type that holds just numeric values.
There are two basic ways to handle this:
1) Set it to a "useless" value - that could be zero, decimal.MinValue, or decimal.MaxValue. You can then test to see if it is that specific value, and ignore it if necessary.
2) Use a nullable decimal:
decimal? test3 = null;
if (test3.HasValue)
{
decimal val = test3.Value;
...
}
请参阅可空类型(C#编程指南) [ ^ ]获取更多信息。
See Nullable Types (C# Programming Guide)[^] for more info.
您不能将字符串值分配给十进制类型变量
我希望这可以帮助您。
< br $>
string test1 = string.Empty;
decimal test2 = decimal.Parse(test1 == string。空?0.00:test1);
You cannot assign a string value to decimal type variable
I hope this may help you.
string test1 = string.Empty;
decimal test2 = decimal.Parse(test1==string.Empty? "0.00":test1);
这篇关于如何传递空十进制值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文