+ 1

double, float or decimal?

I´ve started learning C#, but I´m very confused. I can´t seem to wrap my head around how and where to use the different date types of "floating point values". I´m used to Python, so this is totally different. If anyone have some great tips for understading this, and how to easily convert user-inputs to these data types, that would be great:)

14th Mar 2020, 12:14 PM
Anders Haarberg Eriksen
Anders Haarberg Eriksen - avatar
4 ответов
+ 5
When your program needs very large and accurate numbers than you can use decimal like financing Program,it has longer range than float and double. Decimal is 16 bytes in memory size. Where use of double is commen in every program, it is 8 bytes in memory. And float(4 bytes) is smaller and less in memory size than compared to double and decimal. Float is mostly used in graphic libraries because of it's processing power. Float is faster than double because of smaller range and memory size.
14th Mar 2020, 1:36 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 5
Curious fact is that double can represent much larger numbers (10^308) than decimal (10^28), however decimal can have much higher precision (more valuable digits). Apart from the difference in performance and storage size, there is also the question of static typing. In Python you don't necessarily need to specify if a number is int or float, you just assign the value to a variable. This is dynamic typing. But in C# every variable, and each return value of a method, has a specific data type that you cannot change. Your variable type also must match the parameters and return types of any built-in method that you use. For example Math.Pow() takes two double arguments and returns a double. If you use other types, the compiler may be able to convert it implicitly or you can cast / convert it to a different datatype, of you get an error. So you have to pay attention which type is required and returned by builtin functions. https://exceptionnotfound.net/decimal-vs-double-and-other-tips-about-number-types-in-net/
14th Mar 2020, 1:53 PM
Tibor Santa
Tibor Santa - avatar
+ 2
Oh! Thanks for the quick answer 0_O:)
14th Mar 2020, 1:49 PM
Anders Haarberg Eriksen
Anders Haarberg Eriksen - avatar
+ 2
Great advice Tibor Santa; thanks!
14th Mar 2020, 1:56 PM
Anders Haarberg Eriksen
Anders Haarberg Eriksen - avatar