Does StringToFloat / StringToFloatDef change "," to "." ?? lazarus / 21-General ---------------------------------------------------------------------- Weitentaaal · 2021-02-17 15:38 Hello Got a TEdit from wich i get a Float. So if the user puts 14,4 in does Lazarus change the "," to a dot or do i have to do that manually ? Thanks ---------------------------------------------------------------------- Peter H · 2021-02-17 16:11 It uses the formatsettings and these, if not programmatically changed, depend from locale. In germany we have "," as decimalseparator, but because my programs usually must output ".", I changed the formatsettings. However for copy & paste compatibility with other software my programs must accept both "." and "," for input and I use own routines for this. ---------------------------------------------------------------------- Weitentaaal · 2021-02-17 16:25 looked formatsettings up but did't found something helpful. Where exactly can i find those settings ? in jcfsettings.cfg? Lg Loch ---------------------------------------------------------------------- Peter H · 2021-02-17 16:40 https://www.freepascal.org/docs-html/rtl/sysutils/strtofloatdef.html https://www.freepascal.org/docs-html/rtl/sysutils/tformatsettings.html There is also a global variable that contains the default settings and can be globally changed. I dont remember the details, should be available via Wiki and Google. ---------------------------------------------------------------------- wp · 2021-02-17 16:42 FormatSettings is defined in the SysUtils unit. Here is an untested string-to-float conversion routine which accepts both comma and dot: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function MyStrToFloat(s: String): Double;var  fs: TFormatSettings;begin  if not TryStrToFloat(s, Result) then  begin    fs := FormatSettings;    // Use the "other" decimal separator    if fs.DecimalSeparator = '.'  then fs.DecimalSeparator := ',' else fs.DecimalSeparator := '.';    Result := StrToFloat(s, fs);  end;end; ---------------------------------------------------------------------- winni · 2021-02-17 18:26 Hi! And here is a simple solution like we did it 30 years ago: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function Str2Double ( s : string) : double;var p,error : integer;beginp := pos(',',s);if p > 0 then s[p] := '.';val(s,result,error);if error <> 0 then result := MaxDouble; //or whateverend;  Winni ---------------------------------------------------------------------- lucamar · 2021-02-17 18:51 Quote from: wp on February 17, 2021, 05:42:01 pmHere is an untested string-to-float conversion routine which accepts both comma and dot: [some code] Note that (Try)StrToFloat might fail for reasons other than a bad decimal separator, so you should use TryStrToFloat both times: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function MyStrToFloat(s: String): Double;var  fs: TFormatSettings;begin  if not TryStrToFloat(s, Result) then  begin    fs := FormatSettings;    // Use the "other" decimal separator    if fs.DecimalSeparator = '.'  then fs.DecimalSeparator := ',' else fs.DecimalSeparator := '.';    if not TryStrToFloat(s, Result) then      {some other thing went wrong, e.g. not (just) a number: "$3.95"       Do something about it ...}  end;end; ---------------------------------------------------------------------- wp · 2021-02-17 19:00 I know but I wanted the MyStrToFloat to behave like StrToFloat which does fire an exception when the string is not a valid number. The first conversion with TryStrToFloat has the purpose to check for the correct decimal separator. When it is not successful there may be a wrong decimal separator (which is handled in the following part) or a completely invalid numeric string, and this will raise the exception even after changing the decimal separator. ---------------------------------------------------------------------- marcov · 2021-02-17 20:03 I wouldn't just set decimalseparator to '.'  on a random locale. It might cause problems if the thousands separator also contains '.' (?!?!) Rather, I have two local formatsettings global vars that I fill with getlocaleformatsettings()  (windows only, don't know the *nix eq) for locales that I know (e.g. Dutch or German which uses ',' and English that uses '.' ), and then use those. Then I do a double trytostr on them, as the other code does. ---------------------------------------------------------------------- winni · 2021-02-17 21:11 Quote from: marcov on February 17, 2021, 09:03:27 pmI wouldn't just set decimalseparator to '.'  on a random locale. It might cause problems if the thousands separator also contains '.' (?!?!) Rather, I have two local formatsettings global vars that I fill with getlocaleformatsettings()  (windows only, don't know the *nix eq) for locales that I know (e.g. Dutch or German which uses ',' and English that uses '.' ), and then use those. Then I do a double trytostr on them, as the other code does. Hi ! That  is not an argument. Keep the representation of data and the data for computing separat. The thousands separator only exists because the dumb humans can't see at once if it is one or 10 millions. A string with a thousands separator in any Str2Float crashes. And you should not even try it - see above. Winni ---------------------------------------------------------------------- af0815 · 2021-02-18 06:26 There should be a GuessTryStrToFloat in FPC :-) ---------------------------------------------------------------------- Weitentaaal · 2021-02-18 07:14 How about a KeyPress Event wich Fires when the User inputs ',' and changes it to dot. So the User has no chance to input wrong data. should work right ? ---------------------------------------------------------------------- Peter H · 2021-02-18 07:45 Quote from: Weitentaaal on February 18, 2021, 08:14:36 amHow about a KeyPress Event wich Fires when the User inputs ',' and changes it to dot. So the User has no chance to input wrong data. should work right ? Would this work with copy and paste? I used this method: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormCreate(Sender: TObject);var nn: integer;dt:TDateTime;begin  DefaultFormatSettings.DecimalSeparator := '.';  // SPICE Float Format....end;  procedure MyStrToFloat(S :String; var Value: Double);var  n: double;  i: integer;begin  for i := 1 to length(S) do      if s[i] = ',' then S[i] := '.';  if TryStrToFloat(S, n) then      Value := n;end;.............procedure TForm1.edtAmplitudeEditingDone(Sender: TObject);begin  MyStrToFloat(edtAmplitude.Text,amplitude);  edtAmplitude.Text := FloatToStr(amplitude);end; If the user input cannot be converted, the old value is restored- I dont use thousands separators, so I dont care. ---------------------------------------------------------------------- wp · 2021-02-18 09:05 I strongly advise against modifying the DefaultFormatSettings in an application if there is some chance that anybody different from you will use your application. It's against all conventions and confuses the user. But it even other applications if you allow interaction via clipboard: In my country, the decimal separator is a comma. I wrote a small demo with two edits, one containing the text '1.23', the other one containing '1,23'. At runtime I selected each text and copied it into the clipboard by pressing CTRL+C. Although both values are only copied as text the receiving application into which the clipboard is pasted has its own preferences. When I paste the edit with '1,23' into Excel the text is immediately converted to a floatingpoint value 1.23. And when I paste the edit with '1.23' into Excel, the sometimes hard to understand Excel logic comes into play, and the text is displayed as 'Jan 23'. So, the receiving application does use the decimal separator of the system settings, it ignores your own preferences in your program. ---------------------------------------------------------------------- Peter H · 2021-02-18 09:45 This is intended. I use for example TINA TI, a SPICE Simulator. On the user surface it expects and outputs a comma, but in the scripts a point must be used or is output. It is a horrible mixture ;-) Some programs crash if the wrong separator is input. Then I use other software and set it to english language (or only english is the only language) , because all literature, websites and books are in english. In technical and scientific software and web applets it is common to use "point", so I prefer this if possible. ---------------------------------------------------------------------- wp · 2021-02-18 10:05 When you prefer the dot, why don't you change the language settings of your OS accordingly? Then all programs everything would be fine for you, you could keep the DefaultSettings.DecimalSeparator and avoid confusion of your users. In my opinion, a gui program must stick to the language settings of the system. When it has scripting capabilities it must use the decimal point inside the script, otherwise the script would be locale- dependent. Script parameters entered in the gui must use the Formatsettings decimalseparator, in my opinion it should however be tolerant in case of separtors of the "other" kind. But anyway, it must convert to do..inserted in the script. I agree that all this is a mess. But when you change DefaultSettings you add to this mess. I doubt that writers of software which forces the user to enter floats with a decimal point are aware of this problem. ---------------------------------------------------------------------- Peter H · 2021-02-18 10:19 Quote from: wp on February 18, 2021, 11:05:05 amWhen you prefer the dot, why don't you change the language settings of your OS accordingly? Then all programs everything would be fine for you, you could keep the DefaultSettings.DecimalSeparator and avoid confusion of your users. that writers of software which forces the user to enter floats with a decimal point are aware of this problem. I use "normal" Software also of course with finances and in correspondence, I use comma. I write programs mainly for own use, I am not in job anymore. However if people work in industry using software and measuring instruments, they are used that they must use "." often and are not confused. The program accepts both, comma and point as input and it outputs point only, which is necessary to generate tables for SPICE, so where is a problem? ---------------------------------------------------------------------- wp · 2021-02-18 10:29 I agree with very special software. But even these users will have problems when they copy their measurement data with decimal points to Excel requiring a decimal comma to create a chart for a Powerpoint presentation, and blame Microsoft that the chart axis is suddenly labelel with "Jan 23" instead of "1.23" ---------------------------------------------------------------------- Ally · 2021-02-18 11:52 Hello, my component DecimalNumberEdit is a component for entering numbers. The component is available in the "Online Package Manager" under "RhsPack". The input field only accepts valid numbers that can be limited using the Matrix property. The component provides four new properties. 1. Matrix (string)    The number format is defined via this property.    Examples:    99.99 Allows you to enter values in the range from 0 to 99.99 with two decimal places.    -50.9 Allows you to enter values in the range from -50.9 to 50.9 with one decimal place.    250.000 Allows you to enter values in the range from 0 to 250 with three decimal places.    -500 Allows you to enter values in the range from -500 to 500 without decimal places. 2. Value (Extended)    Returns the extended value of the entered value.    This value can be evaluated during input and represents the value currently entered in the input field. 3. EnterKey (Boolean)    Allows the Enter key to be used in the same way as the Tab key. 4. WarningTone (Boolean)    Outputs a warning tone when illegal entries are made. Under Windows the localization settings such as the DecimalSeparator is automatically adopted. Should the project also use the localization settings under Linux / Unix, you should add the entry clocale to the project uses. Example: uses   {$IFDEF UNIX}{$IFNDEF DARWIN}    clocale, // Sets the localization settings (number, date and time formats)   {$ENDIF}{$ENDIF}   SysUtils, Forms, ...... greetings Roland ---------------------------------------------------------------------- Peter H · 2021-02-18 12:11 Does it support scientific format, e.g. -1.23e-6 ? BTW, on output I would appreciate engineering format for output, that means the exponent is always a multiple of 3. ---------------------------------------------------------------------- winni · 2021-02-18 12:22 Quote from: Ally on February 18, 2021, 12:52:06 pmHello, my component DecimalNumberEdit is a component for entering numbers. The component is available in the "Online Package Manager" under "RhsPack". The input field only accepts valid numbers that can be limited using the Matrix property. The component provides four new properties. Hi! Does your  component care about strings which are entered by paste? This is heavily used in the office. Winni ---------------------------------------------------------------------- marcov · 2021-02-18 12:26 Quote from: winni on February 17, 2021, 10:11:47 pmThat  is not an argument. Keep the representation of data and the data for computing separat. This _is_ about input validation. And over here, both Dutch (which uses decimalsep=',' and thousands ='.') and UK/US versions are in use of both Windows and Office.  Sometimes even within one company (e.g. Dutch for most, but engineers and IT using UK or US etc). Or Office and OS locales that don't match. So that you get non locale encodings in exported Excel CSV files. And user education is hard, specially when they are customers.  I agree with WP: be extremely reluctant to change default encoding. It is almost never a good idea. Quote from: af0815 on February 18, 2021, 07:26:39 am There should be a GuessTryStrToFloat in FPC :-) In my libs it is called "ConvertSingleLocaleIndependent". But while it seems to works fine for me with a mix of European mainland (Dutch,Danish,German,French,Italian) locales and US/UK ones, I've been reluctant to put in in e.g. strutils because it might not work for any arbitrary locale. Also which locales to preload, and on *nix the situation is more complicated because it doesn't have getlocaleformatsettings().   It is just hard to do right in the general case, but if you know a limited number of locales in play, it seems to be fine. ---------------------------------------------------------------------- Peter H · 2021-02-18 12:44 Quote be extremely reluctant to default encoding. It is almost never a good idea. If Writeln and so on is used, is there another way? And because functions like Str() (used in the DMath unit) ignore the Defaultsettings "en" or "us" conventions are in many cases the easiest and securest way. It is just lingua franca in the world. Or think about "CSV" comma separated values, a format that is still widely in use. ---------------------------------------------------------------------- wp · 2021-02-18 13:22 Quote from: Peter H on February 18, 2021, 01:44:36 pmIf Writeln and so on is used, is there another way?Not directly. Of course, you can use the SysUtils conversion routine (FloatToStr etc) which take care of locale. You should be aware that WriteLn and Str are ancient routines written when nobody thought of the possibility that once computers would be found in every household, and when the only computer language was English, and therefore, nobody thought of localization then. QuoteAnd because functions like Str() (used in the DMath unit) ignore the Defaultsettings "en" or "us" conventions are in many cases the easiest and securest way. It is just lingua franca in the world. This is correct for data files, or maybe commandline programs. But definitely wrong for gui programs, at least those which leave your own computer and can be used by other people. ---------------------------------------------------------------------- Ally · 2021-02-18 13:28 @Peter H No, only numbers with a decimal separator. @winni Yes. The validity is also checked when inserting. ---------------------------------------------------------------------- marcov · 2021-02-18 13:37 Quote from: Peter H on February 18, 2021, 01:44:36 pmQuote be extremely reluctant to default encoding. It is almost never a good idea. If Writeln and so on is used, is there another way? Writeln's floating point output is rarely used in applications, since it goes to exponential notation too quickly. QuoteAnd because functions like Str() (used in the DMath unit) ignore the Defaultsettings "en" or "us" conventions are in many cases the easiest and securest way. Then have DMath fixed. Buggy existing code is no excuse to make everything buggy then. For a limited local set of scripts, it is of course ok, and I'd be lying if I would say I never would for limited own use scripts. But as a general rule? Not even close. Don't let a little tech bubble misguide you. There is a real world out there. QuoteIt is just lingua franca in the world. And the Frankish language is Dutch (or alternately Afrikaans or Luxembourgish). But certainly not English :-) (*) QuoteOr think about "CSV" comma separated values, a format that is still widely in use. Everybody says "CSV", but rarely ever means that in the CSV standardized way. Rather, in such informal talk whatever Excel outputs is what is considered "CSV", and for locales with comma as decimal separators, Excel exports files semicolons as field separator. This ambiguity btw makes it extremely hard to have e.g. a "csv" file export from a webpage and have it open reliably in Excel (or at least it was 15 years ago, when I last tried). (*) Dutch Linguist joke, please do not take seriously.