Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert the double value to string without losing the data in c#

I have a double value as below

double propertyValue = 1994.7755474452554;

When i convert this double value to a string

string value = propertyValue.ToString();

it gives the value "1994.77554744526"

It has rounded off the last digits 2554 to 26.

But I do not want to round the value. Share your ideas.

like image 389
Kalai Avatar asked Oct 21 '25 05:10

Kalai


2 Answers

By default the .ToString() method of Double returns 15 digits of precision. If you want the full 17 digits that the double value holds internally, you need to pass the "G17" format specifier to the method.

String s = value.ToString("G17");

This will prevent the rounding off of double value when converted to string.

value = propertyValue.ToString("G17");
like image 190
ViVi Avatar answered Oct 24 '25 04:10

ViVi


You could use decimal type instead.

decimal propertyValue = 1994.7755474452554M;
like image 30
Ben Avatar answered Oct 24 '25 06:10

Ben