### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

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

#### Peter H

• Full Member
• Posts: 197
##### Re: [SOLVED] Formating an int64 to a string
« Reply #15 on: January 19, 2021, 01:57:24 am »
Code: Pascal  [Select][+][-]
1.     function Int2Float4(Value: int64): string;
2.     begin
3.       result := Format('%.4n', [value*0.0001]);
4.       result[Length(Result)-4] := '.';
5.     end;
6.
« Last Edit: January 19, 2021, 02:02:20 am by Peter H »

#### jamie

• Hero Member
• Posts: 4344
##### Re: [SOLVED] Formating an int64 to a string
« Reply #16 on: January 19, 2021, 02:32:05 am »
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
The only true wisdom is knowing you know nothing

#### torbente

• Sr. Member
• Posts: 307
##### Re: [SOLVED] Formating an int64 to a string
« Reply #17 on: January 19, 2021, 03:06:39 am »
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.

Ok, if you want to ignore locals..

Code: Pascal  [Select][+][-]
1. Function FixStrFromInt64(Avalue:Int64):String;
2. Begin
3.   Result := IntToStr(AValue);
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

ok, but that looks overly complicated and bug prone..

Look at the last one I posted just for curiosity, three lines !
« Last Edit: January 19, 2021, 03:15:15 am by torbente »
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

#### torbente

• Sr. Member
• Posts: 307
##### Re: [SOLVED] Formating an int64 to a string
« Reply #18 on: January 19, 2021, 03:43:25 am »
Upps!

Code: Pascal  [Select][+][-]
1. Function FixStrFromInt64(Avalue:Int64):String;
2. Begin
3.   Result := IntToStr(AValue);
5.   Insert('.',Result, Length(Result)-3);
6. end;

This function fails with negative numbers...
Noso Cryptocurrency Main Developer
https://github.com/DevTeamNoso/NosoWallet

#### Peter H

• Full Member
• Posts: 197
##### Re: [SOLVED] Formating an int64 to a string
« Reply #19 on: January 19, 2021, 09:21:59 am »
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.
« Last Edit: January 19, 2021, 11:06:00 am by Peter H »

#### Zoran

• Hero Member
• Posts: 1664
##### Re: [SOLVED] Formating an int64 to a string
« Reply #20 on: January 19, 2021, 10:17:14 am »
Upps!

Code: Pascal  [Select][+][-]
1. Function FixStrFromInt64(Avalue:Int64):String;
2. Begin
3.   Result := IntToStr(AValue);
5.   Insert('.',Result, Length(Result)-3);
6. end;

This function fails with negative numbers...

Fixing is straightforward (not tested):
Code: Pascal  [Select][+][-]
1. Function FixStrFromInt64(Avalue:Int64):String;
2. Begin
3.   Result := IntToStr(Abs(AValue));
5.   Insert('.',Result, Length(Result)-3);
6.   if AValue < 0 then begin
7.     if Result[0] = '0' then
8.       Result[0] := '-'
9.     else
10.       Result := '-' + Result;
11.   end;
12. end;

#### dseligo

• Full Member
• Posts: 156
##### Re: [SOLVED] Formating an int64 to a string
« Reply #21 on: January 19, 2021, 08:17:24 pm »
Fixing is straightforward (not tested):
Code: Pascal  [Select][+][-]
1. Function FixStrFromInt64(Avalue:Int64):String;
2.     if Result[0] = '0' then
3.       Result[0] := '-'
Index in Pascal "String" is not zero based, it goes from 1.[/code]
« Last Edit: January 20, 2021, 01:03:47 am by dseligo »

#### jamie

• Hero Member
• Posts: 4344
##### Re: [SOLVED] Formating an int64 to a string
« Reply #22 on: January 19, 2021, 11:43:10 pm »
How to please them all !

Code: Pascal  [Select][+][-]
1. Function IntToFixPoint(AValue:Int64):String;
2. Begin
3.   Result := IntTostr(Abs(AValue));
5.   Insert('.',Result, Length(Result)-3);
6.   If AValue <0 THen Result := '-'+Result;
7. end;
8.

If you would like I have a Currency Helper unit based on MS Excel ways of doing things.
The only true wisdom is knowing you know nothing

#### Kays

• Sr. Member
• Posts: 299
• Whasup!?
##### Re: Formating an int64 to a string
« Reply #23 on: January 20, 2021, 03:32:27 am »
I 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.

[…] 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][+][-]
1. function imageIntegerAsCurrency(const n: int64): shortString;
2. var
3.         x: currency absolute n;
4. begin
5.         writeStr(imageIntegerAsCurrency, x:1:4);
6. end;
« Last Edit: January 20, 2021, 03:29:19 pm by Kays »
Yours Sincerely
Kai Burghardt

#### jamie

• Hero Member
• Posts: 4344
##### Re: [SOLVED] Formating an int64 to a string
« Reply #24 on: January 20, 2021, 03:46:35 am »
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.
The only true wisdom is knowing you know nothing

#### josh

• Hero Member
• Posts: 824
##### Re: [SOLVED] Formating an int64 to a string
« Reply #25 on: January 20, 2021, 03:56:56 am »
just for fun  Div and Mod

Code: Pascal  [Select][+][-]
1.
2. {Global Vars}
3. Var
4.   APowers:Array[0..8] of Int64=(1,10,100,1000,10000,100000,1000000,10000000,100000000);
5.   azeros:string='0000000000000000000000000';
6.
7. ....
8.
10. var asign:String='';
11. begin
12.   if avalue<0 then asign:='-';
13.   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]));
14. end;
15.

use like  int64toformattedstring(4521252125,8,'.');
« Last Edit: January 20, 2021, 04:12:20 am by josh »
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX..
Any posts made from 2015 will be based on Lazarus Trunk.