+ 2

Float vs. int?

When testing some script, I would get an error message every time I wrote something like float x = 3/1.5; or float x = 2.6; but not float x = 3/2; however, 3/2 would come up as 1 instead of 1.5. If int uses only whole numbers, then what's the difference between float and int?

3rd Aug 2016, 7:17 PM
Playjoy “Kungpoww” Of Death
Playjoy “Kungpoww” Of Death - avatar
4 Antworten
+ 3
This is because literals like 1 or 2 are interpreted as int. As a result operations is done with intigers. You cannot divide intiger by float without converting intiger to float first (they are not compatible). On top of that, writing 2.0 or 1.5 is not quite interpreted as float, but as double instead. Try with doubles (same as float but with higher precision - double is a default floating point type in C#). After operations are done, convert to float like here: ...=(float)(2.0/1.5).
3rd Aug 2016, 10:53 PM
Avis
+ 2
Simple Concept Look Here Guys For INT & Float In Many Softwares Coding You Might Have Seen that, Float value="5.0" But the same in integer INT it can be wrotten as Int ="5" Difference:- Float contains value after (.) Eg:- (.something value) Int doesn't contains that, its just a Single value Hope, It Was Helpful , Thanks
25th Oct 2016, 6:18 AM
PRINCE SURESHKUMAR PATEL
PRINCE SURESHKUMAR PATEL - avatar
0
integers (ints) store Whole number values like 1538 or 3 floats on the other hand store decimal values like 49.3 or 1987.57. (doubles are more precise than floats.)
3rd Dec 2016, 9:20 PM
julio
- 3
to make it simple. Floats are Decimal Number while Ints are While Numbers.
3rd Aug 2016, 7:37 PM
Erwin Mesias
Erwin Mesias - avatar