Devis Posted June 29, 2012 Share Posted June 29, 2012 (edited) Hi there, got another problem again lol. Im trying to use a decimal figure in a TObject. For example: If I use this simple program as an example: [scar]Program Test; Var MyDec: Integer; Output: Integer; Begin MyDec:= 8; Output := 10 div (MyDec); Writeln(Output); End.[/scar] Here I'm using an integer which from reading around is only a whole number value, so the output is always rounded. Like here 10 divided by 8 = 1.25 but this code produces 1 as an output. So I read about a bit more and discovered I could use decimals instead. So I changed my code to: [scar]Program Test; Var MyDec: Single; Output: Single; Begin MyDec:= 8; Output := 10 / (MyDec); Writeln(Output); End.[/scar] Which now works. But the problem is I'm defining my output as a "single;" and not an integer. So when I'm trying to use a decimal in a TObject such as: Var MyDec: Single; MyButton: TButton; Begin MyDec:= 2.3; MyButton. Height:= 50 / (MyDec); End. The output being a TObject is already defined as a variable.. So how can I make it use decimals? And not whole integer numbers? Or am I going in the wrong direction with this? Thanks Edited June 29, 2012 by Devis Quote Link to comment Share on other sites More sharing options...
shadowrecon Posted June 29, 2012 Share Posted June 29, 2012 Hi there, got another problem again lol. Im trying to use a decimal figure in a TObject. For example: If I use this simple program as an example: [scar]Program Test; Var MyDec: Integer; Output: Integer; Begin MyDec:= 8; Output := 10 div (MyDec); Writeln(Output); End.[/scar] Here I'm using an integer which from reading around is only a whole number value, so the output is always rounded. Like here 10 divided by 8 = 1.25 but this code produces 1 as an output. So I read about a bit more and discovered I could use decimals instead. So I changed my code to: [scar]Program Test; Var MyDec: Single; Output: Single; Begin MyDec:= 8; Output := 10 / (MyDec); Writeln(Output); End.[/scar] Which now works. But the problem is I'm defining my output as a "single;" and not an integer. So when I'm trying to use a decimal in a TObject such as: Var MyDec: Single; MyButton: TButton; Begin MyDec:= 2.3; MyButton. Height:= 50 / (MyDec); End. The output being a TObject is already defined as a variable.. So how can I make it use decimals? And not whole integer numbers? Or am I going in the wrong direction with this? Thanks How to use a decimal [scar] program New; var E: Extended; // use extended which is also known as a Float in C++ ect. begin E := 5 / 3; Writeln(E); end. [/scar] You cant assign a Objects location as an extended value. Pixels cant be divided on the screen. For instance you cant place something at Point(50.25, 25.23). Simply use the round or truncate the number after you do your math. [scar] procedure PlaceObj(edt: TEdit); begin edt.height := Round(50 / 1.25); // if you want it to go up or down edt.height := Trunc(50 / 1.25); // if you want it to round down to the nearest integer end; [/scar] Quote Link to comment Share on other sites More sharing options...
Devis Posted June 29, 2012 Author Share Posted June 29, 2012 Thanks Worked perfectly! I totaly forgot that I was ending up with uneven numbers for pixels like 20.5 of a pixel lol Quote Link to comment Share on other sites More sharing options...
LordJashin Posted June 29, 2012 Share Posted June 29, 2012 Also note that Div rounds down. To round up you can either add like .5 or w/e you need to get it up or use the Round command. But notice that for Constants you cannot use commands in them. So if you wanted to round a constant up you would have to add .5 or w/e. Tip: USE ANCHORS, and Align uses them too. It helps so much with forms because otherwise when you resize your form everything overlaps. So just size your component using Top, Left, Width, and Height. THEN use anchors and you can resize the form and everything will stay good. And you won't have to use the on resize event. Another cool property is the Position property for forms, you can set it to DesktopCenter, or ScreenCenter, etc. Quote Link to comment Share on other sites More sharing options...
shadowrecon Posted June 29, 2012 Share Posted June 29, 2012 Also note that Div rounds down. To round up you can either add like .5 or w/e you need to get it up or use the Round command. But notice that for Constants you cannot use commands in them. So if you wanted to round a constant up you would have to add .5 or w/e. Why not call a statement like this [scar] I := Round(2 / 3); // This rounds up if past .5 so I = 1 [/scar] instead of [scar] I := (2 div 3) + 0.4 // This method requires you to know how much until it reaches the next integer which // doesnt make alot of since because then things cant be dynamic.. [/scar] Quote Link to comment Share on other sites More sharing options...
LordJashin Posted June 29, 2012 Share Posted June 29, 2012 Why not call a statement like this[scar] I := Round(2 / 3); // This rounds up if past .5 so I = 1 [/scar] instead of [scar] I := (2 div 3) + 0.4 // This method requires you to know how much until it reaches the next integer which // doesnt make alot of since because then things cant be dynamic.. [/scar] I addressed that lol. Also note that Div rounds down. To round up you can either add like .5 or w/e you need to get it up or use the Round command. Well, prefer to use Round, but just putting that there too. No function calls with that, and added in how constants can't have commands like Round in them. Quote Link to comment Share on other sites More sharing options...
shadowrecon Posted June 29, 2012 Share Posted June 29, 2012 I addressed that lol. Well, prefer to use Round, but just putting that there too. No function calls with that, and added in how constants can't have commands like Round in them. Lol you and your constants. He doesn't have any constants even in the scripts =p Im just glad to see people are on the forums! Anyways i think he solved his issue. Quote Link to comment Share on other sites More sharing options...
LordJashin Posted June 29, 2012 Share Posted June 29, 2012 (._.)<コ:彡 Bazooka! Agreed. Quote Link to comment Share on other sites More sharing options...
Devis Posted June 30, 2012 Author Share Posted June 30, 2012 (edited) Ye sorted it out I made a section of code to change my application depending on the screen resolution of the persons computer, that is what I was trying to do before. This is what I came up with if anyone is interested: [scar] var {Screen Resolution Variable's.} ScreenWidth, ScreenHeight: Integer; ResChangeHeight: Extended; ResChangeWidth: Extended; ResChangeFont: Extended; Procedure FontHighCat; begin ResChangeFont :=1; end; Procedure FontMiddleCat; begin ResChangeFont := 1.25; end; Procedure FontLowCat; begin ResChangeFont := 1.55; end; Procedure FontUltraLowCat; begin ResChangeFont := 1.9; end; Procedure ScaleForm; begin ResChangeFont :=0; ResChangeHeight :=0; ResChangeWidth :=0; GetClientDimensions(ScreenWidth, ScreenHeight); WriteLn(IntToStr(ScreenWidth) + 'x' + IntToStr(ScreenHeight)); //Identify and write screen res into Debug. If (ScreenWidth=1600) and (ScreenHeight=900) then Begin WriteLn('Screen Res is 1600 x 900 No need to change!') WriteLn('Font is in High category') ResChangeHeight :=1; ResChangeWidth :=1; FontHighCat; end else Begin If (ScreenWidth >1600) then Begin WriteLn('Screen Res is More than 1600 x 900 - Changing Res!') WriteLn('Font is in High category') ResChangeHeight := 1; ResChangeWidth := 1; FontHighCat; end else Begin If (ScreenWidth <1600) and (ScreenWidth >=1152) then Begin WriteLn('Screen Res is Less than 1600 x 900 - Changing Res!') WriteLn('Font is in Middle category') ResChangeHeight := 900 / ScreenHeight; ResChangeWidth := 1600 / ScreenWidth; FontMiddleCat; end else Begin If (ScreenWidth <1152) and (ScreenWidth >=1024) then Begin WriteLn('Screen Res is Less than 1600 x 900 - Changing Res!') WriteLn('Font is in Low category') ResChangeHeight := 900 / ScreenHeight; ResChangeWidth := 1600 / ScreenWidth; FontLowCat; end else Begin If (ScreenWidth <1024) then Begin WriteLn('Screen Res is Less than 1600 x 900 - Changing Res!') WriteLn('Font is in Ultra Low category') ResChangeHeight := 900 / ScreenHeight; ResChangeWidth := 1600 / ScreenWidth; FontUltraLowCat; end; end; end; end; end; end;[/scar] And then for every Object on the Form I used: [scar] Object.Font.Size := Round (10 / ResChangeFont); //10 Being the Normal font for this object on 1600 x 900 res. [/scar] My resoultion I developed the app on was 1600 x 900 so this alters the app and font depending on the persons screen res. Theres probably a better way to do this as you said about ANCHORS and ScreenPoints but this is just what I came up with as I have no idea how to use ANCHORS and stuff and couldnt figure them out haha Edited June 30, 2012 by Devis Quote Link to comment Share on other sites More sharing options...