Recent

Author Topic: What's wrong with my program?  (Read 4949 times)

Przemyslav

  • New Member
  • *
  • Posts: 44
What's wrong with my program?
« on: December 11, 2021, 05:54:02 pm »
Hello everybody. I want to write program which convert Degrees/Minutes/Seconds to decimal coordinates, but i have a problem with the code. I'm the new one with Lazarus and Delphi language.

My code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var A, B, C, obl1: Real;
  3. begin
  4.   A := StrToFloat(Edit1.Text);
  5.   B := StrToFloat(Edit2.Text);
  6.   C := StrToFloat(Edit3.Text);
  7.   obl1 := (B/60)+(C/3600);
  8.   Label1.Caption := FloatToStr(A) + ',' + FloatToStr(obl1);
  9. end;  


When I input coordinates for example: 41°24'12.2 I have error.

My form and error in attachments.

Could you help? Thank you.

Przemyslav

  • New Member
  • *
  • Posts: 44
Re: What's wrong with my program?
« Reply #1 on: December 11, 2021, 05:54:38 pm »
Error:

Handoko

  • Hero Member
  • *****
  • Posts: 4520
  • My goal: build my own game engine using Lazarus
Re: What's wrong with my program?
« Reply #2 on: December 11, 2021, 06:04:57 pm »
I tested your code, it worked on my computer without error.

Have you tried to use '12.2' and '12,2' ? I guess what you have is decimal separator issue, comma vs period.

Read more:
https://www.freepascal.org/docs-html/rtl/sysutils/strtofloat.html

Przemyslav

  • New Member
  • *
  • Posts: 44
Re: What's wrong with my program?
« Reply #3 on: December 11, 2021, 06:29:51 pm »
With "," it's okay but with "." i have this error. How to block to input "." in Edit?
« Last Edit: December 11, 2021, 06:31:22 pm by Przemyslav »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9981
  • FPC developer.
Re: What's wrong with my program?
« Reply #4 on: December 11, 2021, 06:35:58 pm »
With "," it's okay but with "." i have this error. How to block to input "." in Edit?

There is an allowedchars property iirc.

loaded

  • Hero Member
  • *****
  • Posts: 568
Re: What's wrong with my program?
« Reply #5 on: December 11, 2021, 06:52:53 pm »
This is how i work ;

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Application.UpdateFormatSettings := false;
  4.   DecimalSeparator := '.';
  5. end;
  6.  
  7.  
  8. procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
  9. begin
  10.   if not (Key in [#8, '0'..'9','-',DecimalSeparator]) then    Key := #0;
  11. end;
  12.        
If Ide=Lazarus 2.0.10 32 Bit and Os=Win 10 Home 64 Bit then Get up and do something useful! Because God is the helper of those who start again;

Przemyslav

  • New Member
  • *
  • Posts: 44
Re: What's wrong with my program?
« Reply #6 on: December 11, 2021, 08:06:43 pm »
Thank you very much :)

Bart

  • Hero Member
  • *****
  • Posts: 4689
    • Bart en Mariska's Webstek
Re: What's wrong with my program?
« Reply #7 on: December 11, 2021, 09:31:03 pm »
A few remarks:

Use a TFloatSpinEdit instead of a TEdit. It is designed for what you seem to want.
It has a Value property (type Double), so you do not need to convert text to double yourself.
If the spinners of the control bother you, you can hide them (IIRC).

Alternatively use a TMaskEdit, so you have absolute control over the input.
With the correct mask, a user cannot even paste non-numbers into the control.

Instead of setting Application.UpdateFormatSettings to false, you can also use the string to floeat conversion routines with a TFormatSettings parameter.

Bart

loaded

  • Hero Member
  • *****
  • Posts: 568
Re: What's wrong with my program?
« Reply #8 on: December 12, 2021, 09:11:51 am »
Thank you so much for the information, bart.
Since different scenarios occur when it is necessary to write the data at hand to the file, I found such a solution in time.
But I made a note of what you said, I'll try it when it's convenient.
If Ide=Lazarus 2.0.10 32 Bit and Os=Win 10 Home 64 Bit then Get up and do something useful! Because God is the helper of those who start again;

Przemyslav

  • New Member
  • *
  • Posts: 44
Re: What's wrong with my program?
« Reply #9 on: December 12, 2021, 05:44:24 pm »
Thank you everybody for help. Okay I will try to use TFloatSpinEdit :)

 

TinyPortal © 2005-2018