Convert double to 4 decimal
Hi,
All the data that I retrieve from database contain 2 decimal, how can I convert it to 4 decimal? For example, one of the data retrieve from database is 172.28, but I would like to convert it to 4 decimal point which is 172.2800.
Code:
double unitprice = Convert.ToDouble(dtLoop1.Tables[0].Rows[DetailRow1]["UnitPrice"].ToString());
decimal unitprice1 = 0.00m;
unitprice1 = Convert.ToDecimal(unitprice);
unitprice1 = (decimal)(unitprice);
Re: Convert double to 4 decimal
That doesn't make much sense really. When you are working with a 'decimal' you have a 128 bit quantity. There is absolutely no difference between "172.28" and "172.2800", they are the same number. If you need to format the output to 4 decimal places just use a format string in the ToString( ) call:
Code:
MessageBox.Show( myDecimal.ToString( ".0000" ) );
BTW, I am unsure why you create a double (which is a floating point number), convert it to a decimal, and then cast it to a decimal...
Re: Convert double to 4 decimal
You can also use string.Format()
Code:
string.Format("Value: {0:F4}", unitPrice);