Decimal.ToString()
is a method in C# that is called on decimal values. It converts a numeric value that is a decimal to a string.
dec.ToString()
dec
: This is the decimal value that we want to convert to a string.
The value returned is a string representation of dec
.
// use Systemusing System;// create classclass DecimalToString{// main methodstatic void Main(){// create decimal valuesdecimal d1 = 34.5m;decimal d2 = 2.4356345m;decimal d3 = 10.34m;// convert to Strings// and print valueConsole.WriteLine("Value = {0}, type = {1}",d1.ToString(),d1.ToString().GetType());Console.WriteLine("Value = {0}, type = {1}",d2.ToString(),d2.ToString().GetType());Console.WriteLine("Value = {0}, type = {1}",d3.ToString(),d3.ToString().GetType());}}
Here is a line-by-line explanation of the above code:
Lines 10-12: We create variables d1
, d2
, and d3
of type decimal and assign decimal values to them.
Lines 16-26: We convert each value to a string using the ToString()
method. Then we print the converted values and their type using the GetType()
method.
As we can see in the output, the decimal values are converted to strings.