+ 1

type

When I do the following why do i get a message that it needs to be a double? One number after a decimal point isn't float ? float x = 10.2 % 4;

2nd Feb 2019, 8:46 PM
joseph prijs
joseph prijs - avatar
5 Respostas
+ 5
This is what documentation says : By default, a real numeric literal on the right side of the assignment operator is treated as double. Therefore, to initialize a float variable, use the suffix f or F, as in the following example: float x = 3.5F; If you do not use the suffix in the previous declaration, you will get a compilation error because you are trying to store a double value into a float variable. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/float
2nd Feb 2019, 8:59 PM
dρlυѕρlυѕ
dρlυѕρlυѕ - avatar
+ 5
You don't like the suffix? :) Just declare them as double. That's a language specification, that's how you declare float.
2nd Feb 2019, 9:47 PM
dρlυѕρlυѕ
dρlυѕρlυѕ - avatar
+ 1
an integer is 4, a float is 4.0 joseph prijs
2nd Feb 2019, 9:13 PM
Ivan
0
Thanks! So what WOULD be considered a float? Is there anything that could validly be declared as a float without a suffix? (I know an integer could but that doesn't need float...
2nd Feb 2019, 9:06 PM
joseph prijs
joseph prijs - avatar
0
Ok Thanks
4th Feb 2019, 2:00 PM
joseph prijs
joseph prijs - avatar