[SOLVED] Formating an int64 to a string lazarus / 61-Beginners ---------------------------------------------------------------------- torbente · 2021-01-18 17:20 Hi everyone. I want format an integer to a string, but neither format or currency fits what i need, or i am unable to find the way. 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 varMynumber : int64 = 27893204; function Int2Curr(velue:int64):String;Beginresult := Format('%.4n', [value * 0.01]);end; MyString := Int2Curr()MyNumber;  This returns: 2.789,3204 I need a way to get: 2789.3204 decimalseparator := '.'; // point Thousandseparator = ''; // nothing Any idea? ---------------------------------------------------------------------- bytebites · 2021-01-18 18:05 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 Format('%.4f', [t*0.0001]) ---------------------------------------------------------------------- torbente · 2021-01-18 18:13 Quote from: bytebites on January 18, 2021, 07:05:13 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Format('%.4f', [t*0.0001]) 2789,3204 The thousand separator is ok, but ',' is the decimal separator. I need '.' (point) ---------------------------------------------------------------------- sstvmaster · 2021-01-18 18:29 maybe like this: 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 Int2Curr(value: int64):String; var  fs: TFormatSettings;begin  fs.DecimalSeparator := '.'; // set decimal separator  result := Format('%.4f', [value*0.0001], fs); // use the format settings in format functionend;  https://wiki.freepascal.org/Format_function ---------------------------------------------------------------------- winni · 2021-01-18 18:37 Hi! No need to fight with format and local setting. Take a simple solution: 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 var i : int64;    s: string;begin  i := 27893204;  str (i*0.0001 : 8:4,s);  showMessage(s);end;               Winni ---------------------------------------------------------------------- tetrastes · 2021-01-18 20:20 It seems that Codetools need to be updated... (Lazarus 2.0.10, maybe in trunk already) ---------------------------------------------------------------------- winni · 2021-01-18 21:09 Quote from: tetrastes on January 18, 2021, 09:20:35 pmIt seems that Codetools need to be updated... (Lazarus 2.0.10, maybe in trunk already) Hi! Not updated but reanimated: str (anyvar : totaldigits : fracdigits, destString); is an Pascal intrinsic since the 70s. Winni ---------------------------------------------------------------------- wp · 2021-01-18 21:12 Quote from: winni on January 18, 2021, 07:37:00 pmNo need to fight with format and local setting. Take a simple solution: 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 var i : int64;    s: string;begin  i := 27893204;  str (i*0.0001 : 8:4,s);  showMessage(s);end;               Assuming that the OP does not want leading spaces, specifying the ":8" in the str() instruction is not good as a general advice. When there are not enough digits in the input number spaces will be added to the left until the output string has a length of at least 8 characters this way. To avoid this, indendent of the input value, it is better to use the width parameter 0 for the general case: 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   str(i*0.0001:0:4, s); ---------------------------------------------------------------------- winni · 2021-01-18 21:24 Quote from: wp on January 18, 2021, 10:12:44 pm Assuming that the OP does not want leading spaces, specifying the ":8" in the str() instruction is not good as a general advice. When there are not enough digits in the input number spaces will be added to the left until the output string has a length of at least 8 characters this way. To avoid this, indendent of the input value, it is better to use the width parameter 0 for the general case: 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   str(i*0.0001:0:4, s); Hi! This is not true. If there are not enough formating space - here 8 - then str takes all needed digits for the integer part. The formatting of the frac part - here 4 - is respectecd. The logic goes this way: If there are too many total digits then the missing digits are filled up with leading spaces. This way you can arrange a formatted output for a table. This is not true, if there are not enough totaldigits. then the string is enlarge to the left. The frac part is allways respected. Winni ---------------------------------------------------------------------- jamie · 2021-01-18 22:58 My turn  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 MyFixPointStr(AInteger :Int64):String;var  C:Currency;  F:TFormatSettings;  D:Integer;Begin  F := FormatSettings;  F.CurrencyDecimals := 4;  C := AInteger;  C := C / 10000;  Result := CurrToStr(C,F);  D := Pos(F.DecimalSeparator, Result);  {Fix no leading zero's for no fraction or 0 component}  If D = 0 Then Result := Result+F.DecimalSeparator;  While (Length(Result)-Pos(F.DecimalSeparator,Result))<4 Do Result := Result+'0';end; { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin  Caption :=MyFixPointStr(1234454);end; ---------------------------------------------------------------------- wp · 2021-01-18 23:03 Quote from: winni on January 18, 2021, 10:24:48 pmIf there are too many total digits then the missing digits are filled up with leading spaces. That's what I am trying to say.  In a GUI with proportional fonts leading spaces are not wanted usually. And therefore the width parameter 0 is the better choice. ---------------------------------------------------------------------- dseligo · 2021-01-18 23:38 Quote from: jamie on January 18, 2021, 11:58:27 pmMy turn  This isn't correct answer, he specifically asked that decimal separator has to be point, and on my computer your function gives this as a result: 123,4454 I would use this: 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 Int2CurrA(value:int64):String;var fsFormat:TFormatSettings;begin  fsFormat:=FormatSettings;  fsFormat.DecimalSeparator:='.';  Result:=FormatFloat('0.0000',value/10000,fsFormat);end;  Or simply this: 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 Int2CurrB(value:int64):String;begin  Result:=IntToStr(value mod 10000);  Result:=IntToStr(value div 10000)+'.'+AddChar('0',Result,4);end; ---------------------------------------------------------------------- jamie · 2021-01-19 00:23 Ok, if you want to ignore locals.. 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 FixStrFromInt64(Avalue:Int64):String;Begin  Result := IntToStr(AValue);  result :=  AddChar('0',Result, 5);  Insert('.',Result, Length(Result)-3);end; procedure TForm1.Button1Click(Sender: TObject);begin  Caption :=FixStrFromInt64(123558900);end;                                     No MOD, NO DIV and it makes sure you have all your zeros ---------------------------------------------------------------------- torbente · 2021-01-19 00:31 I Solved it in the old and good way 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 Int2Float4(Value: int64): string;var  OldStyledecimal, OldStyleThousand : char;beginOldStyledecimal := DefaultFormatSettings.DecimalSeparator;OldStyleThousand:= DefaultForma tSettings.ThousandSeparator;DefaultFormatSettings.DecimalSeparator:='. ';DefaultFormatSettings.ThousandSeparator:=chr(0);result := Format('%.4n', [value*0.0001]);DefaultFormatSettings.DecimalSeparator :=OldStyledecimal;DefaultFormatSettings.ThousandSeparator:=OldStyleTho usand;end; ---------------------------------------------------------------------- jamie · 2021-01-19 00:36 ok, but that looks overly complicated and bug prone.. Look at the last one I posted just for curiosity, three lines ! ---------------------------------------------------------------------- Peter H · 2021-01-19 00:57 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 Int2Float4(Value: int64): string;    begin         result := Format('%.4n', [value*0.0001]);      result[Length(Result)-4] := '.';    end; ---------------------------------------------------------------------- jamie · 2021-01-19 01:32 When ever you introduce anything that involves floating points you then run the risk of inexact values. In this case multiply a number times 0.0001 etc obviously produces a float. But nice shot with only two lines ---------------------------------------------------------------------- torbente · 2021-01-19 02:06 Wow, i really do not take note of your solution. Just amazing. I will use it! It can be used for more or less decimals just adjunting the numbers -3 and 5! Thanks a lot. Quote from: jamie on January 19, 2021, 01:23:13 amOk, if you want to ignore locals.. 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 FixStrFromInt64(Avalue:Int64):String;Begin  Result := IntToStr(AValue);  result :=  AddChar('0',Result, 5);  Insert('.',Result, Length(Result)-3);end; procedure TForm1.Button1Click(Sender: TObject);begin  Caption :=FixStrFromInt64(123558900);end;                                     No MOD, NO DIV and it makes sure you have all your zeros Quote from: jamie on January 19, 2021, 01:36:13 amok, but that looks overly complicated and bug prone.. Look at the last one I posted just for curiosity, three lines ! ---------------------------------------------------------------------- torbente · 2021-01-19 02:43 Upps! 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 FixStrFromInt64(Avalue:Int64):String;Begin  Result := IntToStr(AValue);  result :=  AddChar('0',Result, 5);  Insert('.',Result, Length(Result)-3);end; This function fails with negative numbers... ---------------------------------------------------------------------- Peter H · 2021-01-19 08:21 Cant you use the solution with the str() intrinsic? str() does not use the locale and always produces a decimal point. We have here "," as decimal separator, however quite often "." is needed in technical or scientific computing, for compatibility with international software or measuring instruments, therefore I know it. ---------------------------------------------------------------------- Zoran · 2021-01-19 09:17 Quote from: torbente on January 19, 2021, 03:43:25 amUpps! 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 FixStrFromInt64(Avalue:Int64):String;Begin  Result := IntToStr(AValue);  result :=  AddChar('0',Result, 5);  Insert('.',Result, Length(Result)-3);end; This function fails with negative numbers...  Fixing is straightforward (not tested): 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 FixStrFromInt64(Avalue:Int64):String;Begin  Result := IntToStr(Abs(AValue));  result :=  AddChar('0',Result, 5);  Insert('.',Result, Length(Result)-3);  if AValue < 0 then begin    if Result[0] = '0' then      Result[0] := '-'    else      Result := '-' + Result;  end;end; ---------------------------------------------------------------------- dseligo · 2021-01-19 19:17 Quote from: Zoran on January 19, 2021, 10:17:14 amFixing is straightforward (not tested): 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 FixStrFromInt64(Avalue:Int64):String;    if Result[0] = '0' then      Result[0] := '-'Index in Pascal "String" is not zero based, it goes from 1.[/code] ---------------------------------------------------------------------- jamie · 2021-01-19 22:43 How to please them all ! 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 IntToFixPoint(AValue:Int64):String;Begin  Result := IntTostr(Abs(AValue));  result :=  AddChar('0',Result, 5);  Insert('.',Result, Length(Result)-3);  If AValue <0 THen Result := '-'+Result;end;                                        If you would like I have a Currency Helper unit based on MS Excel ways of doing things. ---------------------------------------------------------------------- Kays · 2021-01-20 02:32 Quote from: torbente on January 19, 2021, 01:31:23 amI Solved it in the old and good way […]Yeesh. The good ole way’d be to use colon- separated format specifiers, just as winni already demonstrated above. Quote from: torbente on January 18, 2021, 06:20:13 pm[…] I want format an integer to a string, but neither format or currency fits what i need, or i am unable to find the way. […] Why doesn’t currency fit? It’s just what the doctor ordered: It’s a 64-bit integer scaled by a factor of 10,000. The “trick” is just typecasting. However, (for some inexplicable reason) something like currency(n) does not work, instead: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 imageIntegerAsCurrency(const n: int64): shortString;var        x: currency absolute n;begin        writeStr(imageIntegerAsCurrency, x:1:4);end; ---------------------------------------------------------------------- jamie · 2021-01-20 02:46 yeah, that actually works well.. Guess that is a winner since it does not require the use of StrUtils and I kind of like using currency myself, I made a complete currency helper class just because I use fix point numbers in a few things where I don't want floating point issues. ---------------------------------------------------------------------- Josh · 2021-01-20 02:56 just for fun  Div and Mod 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  {Global Vars}Var   APowers:Array[0..8] of Int64=(1,10,100,1000,10000,100000,1000000,10000000,100000000);  azeros:string='0000000000000000000000000'; .... function int64toformattedstring(avalue:int64; APlaces:byte; ADecPoint:char):String;var asign:String='';begin  if avalue<0 then asign:='-';  result:=asign+inttostr(abs(avalue div APowers[APlaces]))+ADecPoint+leftstr(azeros,APlaces- length(inttostr(abs(avalue mod APowers[APlaces]))))+inttostr(abs(avalue mod APowers[APlaces]));end;                                         use like  int64toformattedstring(4521252125,8,'.');