15,504,830 members
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```

```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());

}
}

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
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?

## Solution 1

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

Don't forget to mark as answer if it helps. :)

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".

## Solution 2

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"));

}
}```