Link to home
Start Free TrialLog in
Avatar of Softtech
Softtech

asked on

Format currency to string

Assuming...

var
  Money : Currency;

How does one convert a TCurrency value to a formatted string, such as...

$x,xxx,xxx.xx

...so that if Money := 1234.5 then the resulting text string would contain '$1,234.50'
Avatar of shenqw
shenqw

procedure TForm1.Button1Click(Sender: TObject);
begin
  CurrencyString:='$';
  CurrencyFormat:=0;
  ThousandSeparator:=',';
  ShowMessage(Format('%.2m',[12345.5]));
end;
ASKER CERTIFIED SOLUTION
Avatar of Asw
Asw

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry Shengw,

When I started your comment was not up

Regards
Asw
My name is shenqw,but many many people ask me shengw.Oh,my god!  ^o^
Hi. You can use delphi CurrToStr, that converts currency according to windows language settings.

function CurrToStr(Value: Currency): string;
from SysUtils unit;

Shenqw: I have the very same problem - they call me Aleg although I'm AleQ
Avatar of Softtech

ASKER

The precision "4" is insufficient for a value such as 1234.50, nonetheless ASW's response comes closest to meeting my needs.

Thank you Shenq, however, the '[1234.50]' did not fulfil my needs, since I was trying to pass a variable, not a static value.
oh,my god!!!

why you not try:

procedure TForm1.Button1Click(Sender: TObject);
var
  Money : Currency;
begin
  Money:=12345.5;
  CurrencyString:='$';
  CurrencyFormat:=0;
  ThousandSeparator:=',';
  ShowMessage(Format('%.2m',[Money]));
end;

Hi Softtech,

Try this

Var
Money : Currency;
begin
Money := 1234.50;
Label1.Caption := FloatToStrF(Money, ffCurrency, 6, 2);

Asw
Shenqw asked: oh,my god!!! why you not try:

My reply:  Why?  Because I'm a novice and you are the expert, and your original response assumed I knew that the variable 'Money' could be placed between [] brackets...which I did not know.