Вопрос

I have a float value like below,

20.0 

I want to convert 20.0 value to string like below,

20.0000

Result must be,

if float 20.0 than string must be 20.0000

if float 10.0 than string must be 10.0000

How can i convert float value(20.0) to string value(20.0000) ?

Any help will be appreciated. Code I have now:

//worksheet.Cells[j, 7].Value = sonuc1.Unit.ToString() != "" ? (CellValue)sonuc1.Unit : ""; var deneme1 = Convert.ToDecimal(sonuc1.Unit); 
var deneme2 = Convert.ToDouble(sonuc1.Unit); 
var deneme3 = double.Parse("0.01000"); 
var deneme = sonuc1.Unit; 
worksheet.Cells[j, 7].Value = sonuc1.Unit.ToString() != "" ? (CellValue)sonuc1.Unit : "";
Это было полезно?

Решение

You need simply enforce result string to show more zeroes if need.

var a = 20.0f; 
a.ToString("00.0000", CultureInfo.InvariantCulture) 
//2 digits before and 4 digits after (.)

Pay attention on fact that in your case the value may not be exactly 20.0, but something like 20.0012. In these cases you need to first convert it to exact 20.0, after format it to string.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top