如何将int转换为货币? [英] How to convert int to currency?

查看:177
本文介绍了如何将int转换为货币?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在与德尔福2009年合作,解决了我的问题,但是我已经得到的答案已经过时了,因为Delphi2009不知道StrtoFloat。我正在问如何将一个整数转换为1900000到1,900,000?

解决方案

您还可以使用格式命令。因为格式需要一个实数,所以将0.0加到整数上有效地将其变成一个扩展类型。

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

正确处理负数,并为用户区域设置添加货币符号。如果不需要货币符号,则设置CurrencyString:='';在电话之前,然后恢复它。

  SavedCurrency:= CurrencyString; 
try
CurrencyString:='';
结果:=格式('%。0m',[intValue + 0.0]));
finally
CurrencyString:= SavedCurrency;
结束

要强制逗号,只需设置ThousandSeparator:=',';

  CurrencyString:='!'; 
ThousandSeparator:='*';
结果:=格式('%。0m',[ - 1900000.0]);

//返回(!1 * 900 * 000)在我的地区。

遮罩中的周期决定了浮动的小数部分将如何显示。由于我以后通过0,所以告诉format命令不包括任何小数部分。格式化格式('%。3m',[4.0])的格式命令将返回$ 4.000。


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

解决方案

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.

这篇关于如何将int转换为货币?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆