SRL Archive - Java Help and Tutorials

How to initialize a Float?


pointer

2008-04-17 10:38:52

hi, i wan't to 'pre-set' a Float, but it always says incompitable types
how do i pre-set a Float?

Yakman

2008-04-17 13:51:52

EvilChicken!
this is the java programming section, he probably isnt talking about scar

in java, decimal numbers are normally treated as doubles, you can make them floats by adding the letter 'F' after it


float hello = 5.34F;

theres a class Color with constructor Color(float red, float green, float blue)
you would use it like this


Color c = new Color(0.5F, 0.5F, 1F);

senrath

2008-04-17 13:54:09

From: EvilChicken!
I don't quite usderstand whay you're doing.
If you want to set a constant float, then:
const
VersionNumber = 0.23

If you want to pre-set a variable float, then
var
VersionNumber: extended;

begin
VersionNumber := 0.2;
WriteLn(FloatToStr(VersionNumber));
end.


I haven't tested it, but it should work.


That is Pascal syntax, not Java.
IIRC, this is what you need to do:

public float name = (number you want here)F; //for a variable

public static final name = (number you want here)F; //for a constant

HOWEVER, I would recommend using the type double instead of float. I don't remember exactly why at the moment, but it does mostly the same job but better.

EvilChicken!

2008-04-17 15:01:05

Owmygod.
I'm so sorry. Gah. >_<

*hides in corner*
*gets back from corner*
*deletes first post*
*goes back to corner*

pointer

2008-04-18 16:37:04

ty, my calculator is now fully complete