关于十进制转换和精度的问题

发布于 2024-10-01 07:16:13 字数 462 浏览 0 评论 0原文


我试图在这里解决一个数据之谜,但我不确定这是我的代码问题还是互联网浏览器问题...它在 IE6 和 IE8 中工作正常,但不知何故,当客户使用它时,数据显示不正确不使用 IE...

decimal? a = 1.0000m;
decimal? b = 0.6999m;
decimal? c = null;
string aDesc = "";
string bDesc = "";
string cDesc = "";
if (a >= (Decimal).8)
  aDesc = "condition A achieved";
if (b >= (Decimal).8)
  bDesc = "condition B achieved";
if (c >= (Decimal).8)
  cDesc = "condition C achieved";

在所有浏览器中,所有字符串都在末尾分配吗?谢谢。

I'm trying to solve a data mystery here but i'm not sure whether it is my code issue or internet browser issue... it works fine in IE6 and IE8 but somehow the data appears incorrectly when it was used by customers who may not be using IE...

decimal? a = 1.0000m;
decimal? b = 0.6999m;
decimal? c = null;
string aDesc = "";
string bDesc = "";
string cDesc = "";
if (a >= (Decimal).8)
  aDesc = "condition A achieved";
if (b >= (Decimal).8)
  bDesc = "condition B achieved";
if (c >= (Decimal).8)
  cDesc = "condition C achieved";

Does all the strings get assigned at the end of this in all browsers? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鲜肉鲜肉永远不皱 2024-10-08 07:16:13

您正在运行 C#,它应该在服务器端执行。它应该是独立于浏览器的。

此外,只有第一个条件为真。第一个值大于 0.8,第二个值显然不大于,并且我相信比较中的可空类型将始终返回 false。

You're running C#, which should be executed on the server side. It should be browser independent.

Also, only the first condition is true. The first value is greater than .8, the second is clearly not greater, and I believe nullable types on a comparison will always return false.

伴我心暖 2024-10-08 07:16:13

注意:您可以使用 .8m 代替 (Decimal).8。它更短而且更干净。

在这种情况下比较 null(如 Nullable 中)在每种情况下都会导致 false。在一个新项目中尝试一下:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        decimal? a = null;
        if (a < .8m)
        {
            MessageBox.Show("Less Than");
        }
        else if (a >= .8m)
        {
            MessageBox.Show("Greater Than or equal to");
        }
        else
        {
            MessageBox.Show("Neither");
        }
    }
}

如果你仔细想想,它是有道理的。 null 表示没有值,这与(或)有很大不同,后者是特定值。与不存在某物的比较没有真正的定义,它不更大,也不更小,因此两者都解析为 false。一个不存在的人比我高吗?不,他不存在。一个不存在的人比我矮吗?不,他不存在。

编辑

此外,您会发现您的第二个条件(正如其他人指出的那样)总是完全错误的。

/编辑

这就是您所看到的,而不是浏览器问题,希望有帮助!

Note: instead of (Decimal).8 you can use .8m. It's shorter and a bit cleaner.

Comparing a null (as in your Nullable) in this situation results in false in every case. Try this in a fresh project:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        decimal? a = null;
        if (a < .8m)
        {
            MessageBox.Show("Less Than");
        }
        else if (a >= .8m)
        {
            MessageBox.Show("Greater Than or equal to");
        }
        else
        {
            MessageBox.Show("Neither");
        }
    }
}

If you think about it, it makes sense. null means an absence of value, which is very different from none (or zero), which are specific values. Comparison against an absence of something has no real definition, it is not larger, nor is is smaller, so both resolve to false. Is a non-existent person taller than me? Nope, he doesn't exist. Is a non-existent person shorter than me? Nope, he doesn't exist.

EDIT

Also, you'll find that your second condition (as pointed out by others) is always flat out false.

/EDIT

That's what you're seeing, not a browser issue, hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文