C#键入十进制的后缀 [英] C# Type suffix for decimal

查看:184
本文介绍了C#键入十进制的后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我正在试图实现什么正确的措辞,所以它可能已经在网上发布了。

I don't know what the correct wording is for what I am trying to achieve so it may already be posted online. Please be kind if it is.

好的,基本上我有这种方法。

Ok so basically I have this method.

public static T IsNull<T>(IDataReader dr, String name, T nullValue)
{
    return Helpers.IsNull(dr, dr.GetOrdinal(name), nullValue);
}

public static T IsNull<T>(IDataReader dr, Int32 index, T nullValue)
{
    if (dr.IsDBNull(index))
    {
        return nullValue;
    }
    else
    {
        return (T)dr.GetValue(index);
    }
}

被称为助手。 IsNull(dr,UnitWholeSale,0d),我得到的错误是无法从double转换为decimal。

Being called as Helpers.IsNull(dr, "UnitWholeSale", 0d) and the error I am getting is "Cannot convert from double to decimal".

现在我知道我可以使用 decimal.Zero ,但有一些方法,我可以简单地去0dec或类似的东西?我只是讨厌那些长的捷径(特别是当你调用20个参数的构造函数时)。

Now I know I can use decimal.Zero but is there some way that I can simply go 0dec or something similar? I just hate those long shortcut values (especially when you are calling a constructor with 20 parameters).

推荐答案

0m 是你如何说(decimal)0 因为 m 是后缀, 十进制

0m is how you say (decimal)0 because m is the suffix that means decimal.

其他后缀为 f for float d for double u for unsigned l for long 。它们可以是大写,也可以是 u 可以与 l 组合使用,以使 ulong

Other suffixes are f for float, d for double, u for unsigned, and l for long. They can be either upper- or lower-case and u can be combined with l in either order to make a ulong.

虽然后缀不区分大小写,但请记住在C#语言规范中所说的内容,第2.4.4.2节:

Although the suffixes are not case-sensitive, keep in mind what it says in the C# language specification, section 2.4.4.2:


作为一种风格,建议在写文字时使用L而不是l类型很长,因为很容易将字母l与数字1混淆。

As a matter of style, it is suggested that "L" be used instead of "l" when writing literals of type long, since it is easy to confuse the letter "l" with the digit "1".

这篇关于C#键入十进制的后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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