Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert float to double

I'm trying to convert Single to Double while maintaining the original value. I've found the following method:

Single f = 5.2F;
Double d1 = f; // 5.19999980926514
Double d2 = Double.Parse(f.ToString()); // 5.2 (correct)

Is this practice recommendable? I don't need an optimal method, but the intended value must be passed on to the double. Are there even consequences to storing a rounded value in a double?

like image 970
toplel32 Avatar asked Apr 07 '15 13:04

toplel32


1 Answers

You could use "decimal" instead of a string.

float f = 5.2F;
decimal dec = new decimal(f);//5.2
double d = (double)dec; //5.2
like image 165
Baldurs Avatar answered Oct 06 '22 16:10

Baldurs