Recent

Author Topic: [EXAMPLE CalcEdit.Caption.AsFloat] Read and write  (Read 241 times)

What I can do

  • Full Member
  • ***
  • Posts: 122
[EXAMPLE CalcEdit.Caption.AsFloat] Read and write
« on: November 05, 2024, 06:56:34 pm »
Lazarus 3.4
OS: Windows 10@64
Project: Testing the CalcEdit

New form application with only a TCalcEdit;

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormActivate(Sender: TObject);
  2. var
  3.   f:Real;
  4. begin
  5.   calcEdit1.Caption:='3,333.33';
  6.   f:=CalcEdit1.AsFloat; //= 0
  7.   calcEdit1.Caption:='3333.33';
  8.   f:=CalcEdit1.AsFloat; //=3333.33
  9. //Note it's the comma
  10.  
  11. end;
  12.  
« Last Edit: November 07, 2024, 07:12:53 pm by What I can do »

wp

  • Hero Member
  • *****
  • Posts: 12465
Re: CalcEdit I think this is a bug
« Reply #1 on: November 05, 2024, 08:10:35 pm »
 The correct way to enter a number in the TCalcEdit is to set the AsInteger or AsFloat property:
Code: Pascal  [Select][+][-]
  1.  CalcEdit1.AsInteger := 2;
  2.  // or
  3.  CalcEdit1.AsFloat := 3.14;

When you enter the number as string via the Caption or Text property you can enter anything, and this is very confusing: CalcEdit1.Text = 'xyz' is detected as an invalid number, and value 0 is assumed, but still the input text is displayed and there is no error message.

You entered the value '3,333.33' in the Text property. Although Lazarus supports the thousand separator, it is meant only for output, i.e. conversion of a number to string. AFAIK there is no built-in method which accepts a thousand separator for input (conversion from string to number). Therefore '3,333.33' is not considered to be a valid number.

 

TinyPortal © 2005-2018