Recent

Author Topic: A note for those using their own language ...  (Read 278 times)

shyub

  • Jr. Member
  • **
  • Posts: 64
A note for those using their own language ...
« on: April 08, 2021, 10:05:08 am »
In Russian, the separation of the integer part of the number from the fractional part is indicated by a comma, in English - by a dot.
The "StrToFloat" function, unlike Windows, considers a comma to be an error.
« Last Edit: April 08, 2021, 10:07:20 am by shyub »

shyub

  • Jr. Member
  • **
  • Posts: 64
Re: A note for those using their own language ...
« Reply #1 on: April 08, 2021, 10:21:43 am »
Just in case, I do it like this:

Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);
  2. var
  3.   f: Single=1.2;
  4.   S: String;
  5. begin
  6.   .........
  7.   S:=FloatToStr(f);
  8.   Znak:=S[2];
  9.   .........
  10. end;

Wherever you need it ...
Code: Pascal  [Select][+][-]
  1. ...........
  2.     k:=Pos('.',S);
  3.     if k<>0 then S[k]:=Znak;
  4.   end;
  5.   Result:=S;
  6. end;

Handoko

  • Hero Member
  • *****
  • Posts: 4158
  • My goal: build my own game engine using Lazarus
Re: A note for those using their own language ...
« Reply #2 on: April 08, 2021, 10:28:47 am »
My country use comma as decimal separator too.

Maybe you don't know you can 'tell' StrToFloat to use what character as decimal separator. This code below causes no error, just warnings on compile-time. Tested on Lazarus 2.0.10.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   S: string;
  4. begin
  5.  
  6.   S := '1.2';
  7.   DecimalSeparator := '.';
  8.   ShowMessage(StrToFloat(S).ToString);
  9.  
  10.   S := '1,2';
  11.   DecimalSeparator := ',';
  12.   ShowMessage(StrToFloat(S).ToString);
  13.  
  14.   S := '1*2';
  15.   DecimalSeparator := '*';
  16.   ShowMessage(StrToFloat(S).ToString);
  17.  
  18. end;

If you interested to learn more, check the documentation.
« Last Edit: April 08, 2021, 10:35:16 am by Handoko »

shyub

  • Jr. Member
  • **
  • Posts: 64
Re: A note for those using their own language ...
« Reply #3 on: April 08, 2021, 10:32:10 am »
Thank you!
Didn't know it worked on Android.

white_zombie

  • New Member
  • *
  • Posts: 13
Re: A note for those using their own language ...
« Reply #4 on: April 08, 2021, 10:36:21 am »
In my country (Spain), the decimal separator is also the comma.

The function "FloatToStr" has an overload where you can specified the format settings, see the next links.

https://www.freepascal.org/docs-html/rtl/sysutils/floattostr.html
https://www.freepascal.org/docs-html/rtl/sysutils/tformatsettings.html

Best Regards

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 9225
  • FPC developer.
Re: A note for those using their own language ...
« Reply #5 on: April 08, 2021, 10:42:58 am »
On *nix (and Android is unixy) you need to add unit clocale as first unit (or at least early) of your mainprogram to enable loading the system locale.

I don't know in how far this is implemented for android, but you could try
« Last Edit: April 08, 2021, 10:51:11 am by marcov »

shyub

  • Jr. Member
  • **
  • Posts: 64
Re: A note for those using their own language ...
« Reply #6 on: April 08, 2021, 10:44:52 am »
Thank you!

 

TinyPortal © 2005-2018