Thread View: borland.public.delphi.language.basm
10 messages
10 total messages
Started by Tommi Prami
Tue, 08 Apr 2008 07:18
Optimized verion of AddThousandSeparator
Author: Tommi Prami
Date: Tue, 08 Apr 2008 07:18
Date: Tue, 08 Apr 2008 07:18
38 lines
2025 bytes
2025 bytes
Hello,
I was looking for very good way to force thousand separators to Integer
(or float) formatted string (in Finlad thousand separators are not
normally used in OS level).
I came up with this (found it with google)
function emdAddThousandSeparator(const S: string; Chr: Char = ' '): string;
var
I: Integer;
begin
Result := S;
I := Length(S) - 2;
while I > 1 do
begin
Insert(Chr, Result, I);
I := I - 3;
end;
end;
Then I was thinking that it could be defined like this
function IntToStrT(const AValue: Integer; AThousandsSeparator: Char = '
'): string;
(and one for float(s) maybe)
Maybe this way it would be more efficient, to convert it directly to
string with forced thousand separator.
Where I could find routine to modify, pure pascal would do, in which I
could implement thousand separator adding. Or does someone have
optimized code for this already (or good way to force separators with
RTL/VCL, with out changing the Thousand Separator char in
?system.pas/sysutils? (where ever it is, can't remember)...
-TP-
Re: Optimized verion of AddThousandSeparator
Author: Remy Lebeau (TeamB)
Date: Tue, 08 Apr 2008 17:06
Date: Tue, 08 Apr 2008 17:06
12 lines
1314 bytes
1314 bytes
"Tommi Prami" <tommi.nospam.prami@poista.ecomond.com> wrote in message news:47fb1c46$1@newsgroups.borland.com... > I was looking for very good way to force thousand separators > to Integer (or float) formatted string (in Finlad thousand > separators are not normally used in OS level). What about using FormatFloat() for that? Gambit
Re: Optimized verion of AddThousandSeparator
Author: Tommi Prami
Date: Wed, 09 Apr 2008 07:14
Date: Wed, 09 Apr 2008 07:14
21 lines
1615 bytes
1615 bytes
Hello, Can I force Thousand separators to it, even system has not set any??? Without changing locale settings etc... -TP- Remy Lebeau (TeamB) wrote: > "Tommi Prami" <tommi.nospam.prami@poista.ecomond.com> wrote in message > news:47fb1c46$1@newsgroups.borland.com... > >> I was looking for very good way to force thousand separators >> to Integer (or float) formatted string (in Finlad thousand >> separators are not normally used in OS level). > > What about using FormatFloat() for that? > > > Gambit > >
Re: Optimized verion of AddThousandSeparator
Author: Remy Lebeau (TeamB)
Date: Wed, 09 Apr 2008 17:31
Date: Wed, 09 Apr 2008 17:31
15 lines
1717 bytes
1717 bytes
"Tommi Prami" <tommi.nospam.prami@poista.ecomond.com> wrote in message news:47fc6cc4$1@newsgroups.borland.com... > Can I force Thousand separators to it, even system has > not set any??? Without changing locale settings etc... Have you read the documentation for FormatFloat() yet? It tells you how to indicate that you want thousand separators to appear in the output. As for the actual separator, the global ThousandsSeparator variable is used. If your system does not define any separator, then the VCL defaults to the ',' character. Gambit
Re: Optimized verion of AddThousandSeparator
Author: Tommi Prami
Date: Thu, 10 Apr 2008 07:39
Date: Thu, 10 Apr 2008 07:39
10 lines
1561 bytes
1561 bytes
> Have you read the documentation for FormatFloat() yet? It tells you how to > indicate that you want thousand separators to appear in the output. As for > the actual separator, the global ThousandsSeparator variable is used. If > your system does not define any separator, then the VCL defaults to the ',' > character. Just What I suspected, so I can't use that. -TP-
Re: Optimized verion of AddThousandSeparator
Author: Stig Johansen
Date: Thu, 10 Apr 2008 08:23
Date: Thu, 10 Apr 2008 08:23
33 lines
2063 bytes
2063 bytes
"Tommi Prami" <tommi.nospam.prami@poista.ecomond.com> wrote in message
news:47fdc42d$1@newsgroups.borland.com...
> > Have you read the documentation for FormatFloat() yet? It tells you how
to
> > indicate that you want thousand separators to appear in the output. As
for
> > the actual separator, the global ThousandsSeparator variable is used.
If
> > your system does not define any separator, then the VCL defaults to the
','
> > character.
>
>
> Just What I suspected, so I can't use that.
Yes you can.
As for the decimal separator i do:
VAR
kDecimalSeparator : Char ; // keep
...
DecimalSeparator := '.' ; // in DK we normally use ','
something := ' Stardate: ' + FormatFloat('#.0',Now) ; // now it uses '.'
as decimal separator
DecimalSeparator := kDecimalSeparator ; // restore system settings.
You just have to do the same with the ThousandsSeparator as Remy
mentions.
--
Med venlig hilsen/Best regards
Stig Johansen
Re: Optimized verion of AddThousandSeparator
Author: Tommi Prami
Date: Thu, 10 Apr 2008 08:44
Date: Thu, 10 Apr 2008 08:44
21 lines
1952 bytes
1952 bytes
> kDecimalSeparator : Char ; // keep
> ....
>
> DecimalSeparator := '.' ; // in DK we normally use ','
> something := ' Stardate: ' + FormatFloat('#.0',Now) ; // now it uses '.'
> as decimal separator
> DecimalSeparator := kDecimalSeparator ; // restore system settings.
>
> You just have to do the same with the ThousandsSeparator as Remy
> mentions.
This is not acceptable, because software is heavily multi threaded and
all other parts must not be affected with this.
And using critical section etc, is not an option here.
I think I need to use someting different. Code I send earlyer works, but
is not very efficient, thats all...
-TP-
Re: Optimized verion of AddThousandSeparator
Author: Soeren Muehlbauer
Date: Thu, 10 Apr 2008 10:13
Date: Thu, 10 Apr 2008 10:13
30 lines
2098 bytes
2098 bytes
Hi,
FormatFloat is an overloaded function and can called with
TFormatSettings. In TFormatSettings you can set the ThousandSeparator.
This is a threadsave way to override the standard behavior.
Soeren
>> kDecimalSeparator : Char ; // keep
>> ....
>>
>> DecimalSeparator := '.' ; // in DK we normally use ','
>> something := ' Stardate: ' + FormatFloat('#.0',Now) ; // now it
>> uses '.'
>> as decimal separator
>> DecimalSeparator := kDecimalSeparator ; // restore system settings.
>>
>> You just have to do the same with the ThousandsSeparator as Remy
>> mentions.
>
> This is not acceptable, because software is heavily multi threaded and
> all other parts must not be affected with this.
>
>
> And using critical section etc, is not an option here.
>
> I think I need to use someting different. Code I send earlyer works, but
> is not very efficient, thats all...
>
> -TP-
Re: Optimized verion of AddThousandSeparator
Author: Tommi Prami
Date: Thu, 10 Apr 2008 12:42
Date: Thu, 10 Apr 2008 12:42
9 lines
1448 bytes
1448 bytes
Ok, i've useed it but never studied he internals... Thanks... btw... Is there (been) FastCode Chanllenge on format float (etc) functions). -TP-
Re: Optimized version of AddThousandSeparator
Author: Carsten Bothe
Date: Thu, 15 May 2008 08:51
Date: Thu, 15 May 2008 08:51
132 lines
4702 bytes
4702 bytes
Little more Performance but full functional i hope:
No Insert String, nor delete String
See : Format('%.*n')
Change '.' to ',' and ',' to '.' or any other char u want
var MyThousandsSeparator:char='.';
MyDecimalSeparator:char=',';
MyCheckSeparators:byte=0;
function double2string(b:double; n:byte):string;
var r:double;
v:double;
z:integer;
begin
{
for rounding currency values
r:=0;
case n of
1:r:=0.001;
2:r:=0.0001;
3:r:=0.00001;
4:r:=0.000001;
5:r:=0.0000001;
6:r:=0.00000001;
end;
if (b<0) then r := -r;
v:=b+r;
}
v:=b;
if n=0 then begin // NoDecimals Just integer
result:=format('%d',[trunc(v)]);
exit; // all done
end else result:=format('%.*n',[n,v]);
if MyCheckSeparators=0 then begin // if you use always the same
separators you might do this in initialization section
MyCheckSeparators:=1; // This will be used ONCE
if (MyThousandsSeparator <> ThousandSeparator) then
MyCheckSeparators:=MyCheckSeparators or 2;
if (MyDecimalSeparator <> DecimalSeparator) then
MyCheckSeparators:=MyCheckSeparators or 4;
end;
// if you use always the same separators you might do this in
initialization section
case MyCheckSeparators of // This compiles to some
additional sub and jnz ops
1:; // Do nothing
3:begin // Change thousend character only ( you may change result[z] to
cP^ and use pointer ops
for z:=1 to length(result) do begin
if result[z]=ThousandSeparator then
result[z]:=MyThousandsSeparator;
end;
end;
5:begin // Chenge decimal only
for z:=1 to length(result) do begin
if result[z]=DecimalSeparator then result[z]:=MyDecimalSeparator;
end;
end;
7:begin // change both
{
MayBe Faster :
cP:=@result[1];
for z:=1 to length(result) do begin
if cP^=DecimalSeparator then begin
cP^:=MyDecimalSeparator;
end else begin
if cP^=ThousandSeparator then cP^:=MyThousandSeparator
end;
inc(cP);
end;
}
for z:=1 to length(result) do begin
if result[z]=DecimalSeparator then begin
result[z]:=MyDecimalSeparator;
end else begin
if result[z]=ThousandSeparator then
result[z]:=MyThousandsSeparator;
end;
end;
end;
end;
end;
"Tommi Prami" <tommi.nospam.prami@poista.ecomond.com> schrieb im Newsbeitrag
news:47fb1c46$1@newsgroups.borland.com...
> Hello,
>
> I was looking for very good way to force thousand separators to Integer
> (or float) formatted string (in Finlad thousand separators are not
> normally used in OS level).
>
> I came up with this (found it with google)
>
> function emdAddThousandSeparator(const S: string; Chr: Char = ' '):
> string;
> var
> I: Integer;
> begin
> Result := S;
> I := Length(S) - 2;
> while I > 1 do
> begin
> Insert(Chr, Result, I);
> I := I - 3;
> end;
> end;
>
>
> Then I was thinking that it could be defined like this
>
> function IntToStrT(const AValue: Integer; AThousandsSeparator: Char = '
> '): string;
> (and one for float(s) maybe)
>
> Maybe this way it would be more efficient, to convert it directly to
> string with forced thousand separator.
>
> Where I could find routine to modify, pure pascal would do, in which I
> could implement thousand separator adding. Or does someone have optimized
> code for this already (or good way to force separators with RTL/VCL, with
> out changing the Thousand Separator char in ?system.pas/sysutils? (where
> ever it is, can't remember)...
>
> -TP-
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads