Do anyone knows how do I format the quantity of displayed fractional numbers of a variable in Delphi (in my program, it is being used the variable Real).
i.e:
3.14159265359
integer value|fractional value
Based on the example, all I want is this number to be displayed as 3.14 (note that this is just an example. In my program, the user is going to input the real value).
I'm looking forward to display this value in a label. Is there any way to do that? If yes, how?
REFERENCE -> link to exemplify exactly what I want to do, but in Delphi (instead of Java, which is the language used in the link below):
Code sample which successfully implements what I was looking forward to do:
Var
a, b, c: Real;
begin
a := StrToFloat(txt1.Text);
b := INT(a);
c := frac(ABS(a)) + 0.000000000000001;
Full code sample:
unit Unit10;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TfrmM4E1 = class(TForm)
btn1: TButton;
lbl1: TLabel;
txt1: TEdit;
lbl2: TLabel;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmM4E1: TfrmM4E1;
implementation
{$R *.dfm}
procedure TfrmM4E1.btn1Click(Sender: TObject);
Var
a, b, c: Real;
begin
a := StrToFloat(txt1.Text);
b := INT(a);
c := frac(ABS(a)) + 0.000000000000001;
lbl1.Caption := FloatToStr(b);
lbl2.Caption := FloatToStr(c);
end;
end.
Quick explanation: this is the code implementation of a UI, which has a button, a text field (which get the fractional value), and two labels, one of them displays the integer portion of the entered number, and the other label, displays the fractional portion of the entered number.
Variable "a" stores the inserted value;
Variable "b" stores the integer value from variable "a";
Variable "c" stores the fractional value from variable "a", plus a number correction since "c" lacks 0.000000000000001 in the displayed final value.
Finally, labels "lbl1", and "lbl2" shows the values of variables "b" and "c", respectivaly.