Click here to Skip to main content
15,504,830 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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


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;
   }
}
Posted
Updated 30-Dec-11 1:23am
v3
Comments
Sergey Alexandrovich Kryukov 30-Dec-11 10:39am    
Why? why?! Why this strange class Variant? Do you think you will be able to create a "universal numeric" type and operations for it? Wrong idea.
--SA
Antonino Porcino 30-Dec-11 10:56am    
Please consider it as an example as it's an extract of a much bigger project. My question actually is: why does (decimal).ToString() give a strange "0,00000" instead of "0". Can you explain why?

just write in this way :
C#
Console.WriteLine(((decimal)V.value).ToString("0.00"));

Hope this will help you.
Don't forget to mark as answer if it helps. :)
 
Share this answer
 
Comments
Antonino Porcino 30-Dec-11 10:57am    
Sorry if I was not clear, I didn't ask how to output "0", I actually want to know why (decimal).ToString() gives "0,00000" instead of "0".
C#
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();
   }
}
 
Share this answer
 
Comments
Antonino Porcino 30-Dec-11 10:58am    
Sorry if I was not clear, I didn't ask how to output "0", I actually want to know why (decimal).ToString() gives "0,00000" instead of "0". I just can't figure out why.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900