十进制被视为字符串的奇怪行为 [英] Strange behaviour with decimal treated as strings

查看:79
本文介绍了十进制被视为字符串的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此感到疯狂!我无法解释为什么下面的代码输出不是

I''m getting crazy about this! I can''t explain why the output of the code below is not

0
0
0



而是:



but it''s instead:

0,0000000000
0,0000
0,0



我看不到将".ToString()"应用于十进制值如何引用用于构建十进制值的参数字符串.

任何帮助是极大的赞赏!

(编辑)可以更好地说明这一点:我要问的是,即使应用的十进制数相同(在所有三种情况下均为0),. ToStrings()仍可能给出3种不同的结果.



I can''t see how the ".ToString()" applied to a decimal value can make reference to the parameter string used to build the decimal value.

Any help is greatly appreciated!

(Edit) to better clarify: what I''m asking is how it''s possible that .ToStrings() gives 3 different results even if the decimal number to which it is applied is the same (0 in all three cases).

class Program
{
   static void Main(string[] args)
   {
      Variant V;         
      V = new Variant("0,0000000000"); 
      Console.WriteLine(((decimal)V.value).ToString());  
      
      V = new Variant("0,0000");       
      Console.WriteLine(((decimal)V.value).ToString());  
      
      V = new Variant("0,0");          
      Console.WriteLine(((decimal)V.value).ToString());  
      
      Console.ReadLine();       
   }
}

public class Variant
{
   public object value;

   public Variant(string s)
   {
         decimal d;
         decimal.TryParse(s,out d);
         value = d;
   }
}

推荐答案

只需以这种方式编写:
just write in this way :
Console.WriteLine(((decimal)V.value).ToString("0.00"));


希望对您有帮助.
如果有帮助,请不要忘记将其标记为答案. :)


Hope this will help you.
Don''t forget to mark as answer if it helps. :)


please try this

class Program
{
   static void Main(string[] args)
   {
      Variant V;
      V = new Variant("0,0000000000");
      Console.WriteLine(((decimal)V.value).ToString("0"));

      V = new Variant("0,0000");
      Console.WriteLine(((decimal)V.value).ToString("0"));

      V = new Variant("0,0");
      Console.WriteLine(((decimal)V.value).ToString("0"));

      Console.ReadLine();
   }
}


这篇关于十进制被视为字符串的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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