Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert int to currency?

Tags:

types

delphi

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'?

like image 814
Ivan Prodanov Avatar asked Dec 30 '22 17:12

Ivan Prodanov


1 Answers

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.

like image 135
skamradt Avatar answered Jan 30 '23 12:01

skamradt