Recent

Author Topic: [SOLVED] Formating an int64 to a string  (Read 3758 times)

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
[SOLVED] Formating an int64 to a string
« on: January 18, 2021, 06:20:13 pm »
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][+][-]
  1. var
  2. Mynumber : int64 = 27893204;
  3.  
  4. function Int2Curr(velue:int64):String;
  5. Begin
  6. result := Format('%.4n', [value * 0.01]);
  7. end;
  8.  
  9. MyString := Int2Curr()MyNumber;
  10.  

This returns: 2.789,3204

I need a way to get:

2789.3204

decimalseparator := '.'; // point
Thousandseparator = ''; // nothing

Any idea?

« Last Edit: January 19, 2021, 01:31:54 am by torbente »
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

bytebites

  • Hero Member
  • *****
  • Posts: 632
Re: Formating an int64 to a string
« Reply #1 on: January 18, 2021, 07:05:13 pm »
Code: Pascal  [Select][+][-]
  1. Format('%.4f', [t*0.0001])

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Formating an int64 to a string
« Reply #2 on: January 18, 2021, 07:13:42 pm »
Code: Pascal  [Select][+][-]
  1. Format('%.4f', [t*0.0001])

2789,3204

The thousand separator is ok, but ',' is the decimal separator. I need '.' (point)
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

sstvmaster

  • Sr. Member
  • ****
  • Posts: 299
Re: Formating an int64 to a string
« Reply #3 on: January 18, 2021, 07:29:35 pm »
maybe like this:
Code: Pascal  [Select][+][-]
  1. function Int2Curr(value: int64):String;
  2. var
  3.   fs: TFormatSettings;
  4. begin
  5.   fs.DecimalSeparator := '.'; // set decimal separator
  6.   result := Format('%.4f', [value*0.0001], fs); // use the format settings in format function
  7. end;
  8.  

https://wiki.freepascal.org/Format_function
« Last Edit: January 18, 2021, 07:36:11 pm by sstvmaster »
greetings Maik

Windows 10,
- Lazarus 2.2.6 (stable) + fpc 3.2.2 (stable)
- Lazarus 2.2.7 (fixes) + fpc 3.3.1 (main/trunk)

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Formating an int64 to a string
« Reply #4 on: January 18, 2021, 07:37:00 pm »
Hi!

No need to fight with format and local setting.

Take a simple solution:

Code: Pascal  [Select][+][-]
  1. var i : int64;
  2.     s: string;
  3. begin
  4.   i := 27893204;
  5.   str (i*0.0001 : 8:4,s);
  6.   showMessage(s);
  7. end;              

Winni

tetrastes

  • Sr. Member
  • ****
  • Posts: 473
Re: Formating an int64 to a string
« Reply #5 on: January 18, 2021, 09:20:35 pm »
It seems that Codetools need to be updated...
(Lazarus 2.0.10, maybe in trunk already)
« Last Edit: January 18, 2021, 09:23:44 pm by tetrastes »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Formating an int64 to a string
« Reply #6 on: January 18, 2021, 10:09:27 pm »
It 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

  • Hero Member
  • *****
  • Posts: 11855
Re: Formating an int64 to a string
« Reply #7 on: January 18, 2021, 10:12:44 pm »
No need to fight with format and local setting.

Take a simple solution:

Code: Pascal  [Select][+][-]
  1. var i : int64;
  2.     s: string;
  3. begin
  4.   i := 27893204;
  5.   str (i*0.0001 : 8:4,s);
  6.   showMessage(s);
  7. 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][+][-]
  1.   str(i*0.0001:0:4, s);

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Formating an int64 to a string
« Reply #8 on: January 18, 2021, 10:24:48 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][+][-]
  1.   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

  • Hero Member
  • *****
  • Posts: 6090
Re: Formating an int64 to a string
« Reply #9 on: January 18, 2021, 11:58:27 pm »
My turn  :D

Code: Pascal  [Select][+][-]
  1. Function MyFixPointStr(AInteger :Int64):String;
  2. var
  3.   C:Currency;
  4.   F:TFormatSettings;
  5.   D:Integer;
  6. Begin
  7.   F := FormatSettings;
  8.   F.CurrencyDecimals := 4;
  9.   C := AInteger;
  10.   C := C / 10000;
  11.   Result := CurrToStr(C,F);
  12.   D := Pos(F.DecimalSeparator, Result);  {Fix no leading zero's for no fraction or 0 component}
  13.   If D = 0 Then Result := Result+F.DecimalSeparator;
  14.   While (Length(Result)-Pos(F.DecimalSeparator,Result))<4 Do Result := Result+'0';
  15. end;
  16.  
  17. { TForm1 }
  18.  
  19. procedure TForm1.Button1Click(Sender: TObject);
  20. begin
  21.   Caption :=MyFixPointStr(1234454);
  22. end;
  23.  
  24.  

The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: Formating an int64 to a string
« Reply #10 on: January 19, 2021, 12:03:57 am »
If 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

  • Hero Member
  • *****
  • Posts: 1196
Re: Formating an int64 to a string
« Reply #11 on: January 19, 2021, 12:38:26 am »
My turn  :D
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][+][-]
  1. function Int2CurrA(value:int64):String;
  2. var fsFormat:TFormatSettings;
  3. begin
  4.   fsFormat:=FormatSettings;
  5.   fsFormat.DecimalSeparator:='.';
  6.   Result:=FormatFloat('0.0000',value/10000,fsFormat);
  7. end;
  8.  

Or simply this:
Code: Pascal  [Select][+][-]
  1. function Int2CurrB(value:int64):String;
  2. begin
  3.   Result:=IntToStr(value mod 10000);
  4.   Result:=IntToStr(value div 10000)+'.'+AddChar('0',Result,4);
  5. end;
  6.  

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Formating an int64 to a string
« Reply #12 on: January 19, 2021, 01:23:13 am »
Ok, if you want to ignore locals..

Code: Pascal  [Select][+][-]
  1. Function FixStrFromInt64(Avalue:Int64):String;
  2. Begin
  3.   Result := IntToStr(AValue);
  4.   result :=  AddChar('0',Result, 5);
  5.   Insert('.',Result, Length(Result)-3);
  6. end;
  7.  
  8. procedure TForm1.Button1Click(Sender: TObject);
  9. begin
  10.   Caption :=FixStrFromInt64(123558900);
  11. end;                                      
  12.  

No MOD, NO DIV and it makes sure you have all your zeros
The only true wisdom is knowing you know nothing

torbente

  • Sr. Member
  • ****
  • Posts: 325
    • Noso Main Page
Re: Formating an int64 to a string
« Reply #13 on: January 19, 2021, 01:31:23 am »
I Solved it in the old and good way

Code: Pascal  [Select][+][-]
  1. function Int2Float4(Value: int64): string;
  2. var
  3.   OldStyledecimal, OldStyleThousand : char;
  4. begin
  5. OldStyledecimal := DefaultFormatSettings.DecimalSeparator;
  6. OldStyleThousand:= DefaultFormatSettings.ThousandSeparator;
  7. DefaultFormatSettings.DecimalSeparator:='.';
  8. DefaultFormatSettings.ThousandSeparator:=chr(0);
  9. result := Format('%.4n', [value*0.0001]);
  10. DefaultFormatSettings.DecimalSeparator :=OldStyledecimal;
  11. DefaultFormatSettings.ThousandSeparator:=OldStyleThousand;
  12. end;  
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: [SOLVED] Formating an int64 to a string
« Reply #14 on: January 19, 2021, 01:36:13 am »
ok, but that looks overly complicated and bug prone..

Look at the last one I posted just for curiosity, three lines ! ;)
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018