C# - Data Type for value 0.5 -


i have calculation example 2/4 = 0.5, except can't find data type store value! every time make calculation says 0.

anyone have suggestions?

either double or decimal work fine.

however, if write:

// wrong double x = 2 / 4; 

then still use integer division - because both of operands division operator of type int.

you can either cast either or both of operands double, or use double literal either or both of them:

// of these... double x = (double) 2 / 4; double x = 2 / (double) 4; double x = (double) 2 / (double) 4; double x = 2.0 / 4; double x = 2 / 4.0; double x = 2.0 / 4.0; double x = 2d / 4; double x = 2 / 4d; double x = 2d / 4d; 

note both double , decimal floating point types. don't represent arbitrary rational numbers (fractions). example, neither can accurately represent 1/3. if need rational number type, you'll have write own or third party implementation.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -