views:

808

answers:

4

Hello,

I'm working with Delphi 2009,I binged my question,but the answers I've gotten are outdated since It doesn't recognise StrtoFloat in Delphi2009.

I'm asking how to convert an integer ,for example, '1900000' to '1,900,000'?

+1  A: 

I currently use this :

function FloatToCurrency(const f: double): string;
begin
  Result := FormatFloat('#,###.##;1;0', f);
end;

It doesn't work with negative numbers, but since you need currency you won't have that problem.

Aldo
@Aldo,Thanks for answer.My code: 'sLabel1.Caption := FormatFloat('#,###.##;1;0',StrToCurr(IntToStr(1900000)));' It display '1 900 000' where are the commas?
John
Edit: I used StrToCurr(IntToStr()) because the number I use in my application is Int64
John
Currency should *NEVER* be a float in the first place except as a conversion during display.
Loren Pechtel
Loren,please suggest something that works.
John
Why never? How should one display I have 55.3 which I want to display as currency for example. Oh and by the way that code you posted works John.
Aldo
I agree with those who suggest that currency be stored as integer typed in the lowest common denominator. But I think any native `decimal` type (don't know about Delphi) will be better than float.
maxwellb
Doubles suffer from known rounding problems. There is a CURRENCY type which is a mutant but if you don't need fractional dollars then integer is by far the best choice.
skamradt
Money should always be stored as an integer type in units of the smallest unit of currency. Thus in the US you store the number of cents, not the number of dollars. I won't even use the CURRENCY type as the extra decimals will cause rounding problems if you ever multiply by a fraction (say, percentages...)
Loren Pechtel
A: 

wouldnt this be more of a format thing, delphi should have some type of support for formating the number into a string the way you want right? Besides isnt the newer versions of delphi more aligned with the .net framework?

mxrss
Delphi 2009 is for native win32 development.Delphi prism is for net.
John
+4  A: 

You can also use the format command. Because the format expects a real number, adding 0.0 to the integer effectively turns it into an extended type.

Result := Format('%.0m',[intValue + 0.0]));

This handles negative numbers properly and adds the currency symbol for the users locale. If the currency symbol is not wanted, then set CurrencyString := ''; before the call, and restore it afterwards.

SavedCurrency := CurrencyString;
try
  CurrencyString := '';
  Result := Format('%.0m',[intValue + 0.0]));
finally
  CurrencyString := SavedCurrency;
end;

To force commas, just set the ThousandSeparator := ',';

CurrencyString := '!';
ThousandSeparator := '*';
Result := Format('%.0m',[-1900000.0]);  

// Returns (!1*900*000) in my locale.

The "period" in the mask determines how the fractional portion of the float will display. Since I passed 0 afterwards, it is telling the format command to not include any fractional pieces. a format command of Format('%.3m',[4.0]) would return $4.000.

skamradt
You're my Delphi Idol,you know that! But I have a problem this time,It doesn't display ',' commas.Instead just a space.I had this problem in C# too and I asked about it awhile ago while I was working in C#.People told me its a currencyFormat problem and I remember there was a way to do that in C#.Don't know about delphi.I only want commas,it doesnt have to show anything else and knowing this wont work on every currencyType,could you suggest something that puts commas? Thank you in advance! wish I could vote more!
John
Thank you for your edit,but I misunderstand how to set ThousandSeparator.If i use '%,0m' it throws an exception.
John
edited with an extreme example.
skamradt
Great,you're a delphi legend!
John
+1  A: 

You can assign Integer to Currency directly by assignment, the compiler will do the conversion for you:

var
  Int : Integer;
  Cur : Currency;
begin
  Int := 1900000;
  Cur := Int;
  ShowMessage(CurrToStr(Cur)); // 1900000
  ShowMessage(Format('%m', [Cur]); // 1,900,000.00 in US/UK/NZ/AU etc, "1 900 000,00" in Spain etc.
  ShowMessage(Format('%.0m', [Cur]); // 1,900,000 in US/UK/NZ/AU etc, "1 900 000" in Spain etc.
end;

If you want Commas using Spanish regional settings set ThousandSeparator := ','; or use the extended CurrToStrF(amount, ffCurrency, decimals, FormatSettings)) version.

The verison with FormatSettings is also thread-safe.

Note: You can't assign Currency to Integer directly, You would need to use Int := Trunc(Cur) but this is inefficient as it converts to float first (unless compiler does something smart).

Gerry