delphi - 如何将 int 转换为货币?

标签 delphi types

我正在使用Delphi 2009,我问了我的问题,但我得到的答案已经过时了,因为它无法识别Delphi2009中的StrtoFloat。

我问如何将整数转换为“1,900,000”,例如“1900000”?

最佳答案

您还可以使用格式命令。由于格式需要实数,因此向整数添加 0.0 可有效地将其转换为扩展类型。

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

这可以正确处理负数并添加用户区域设置的货币符号。如果不需要货币符号,则设置CurrencyString := '';调用前,调用后恢复。

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

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

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

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

掩码中的“句点”决定了 float 的小数部分的显示方式。由于我后来传递了 0,它告诉 format 命令不要包含任何小数部分。 Format('%.3m',[4.0]) 的格式命令将返回 $4.000。

关于delphi - 如何将 int 转换为货币?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1246770/

相关文章:

ios - 为什么 Swift 不将此 Int 数组文字转换为正确的类型?

delphi - 迁移到 Delphi 2009 - 无效类型转换错误

html - HTML 字符实体和 Delphi 之间的数字等价性?

typescript - 在 Typescript 中定义函数类型、函数接口(interface)

python - pandas.value_counts 不适用

typescript - 如何使TS编译器知道未知变量具有某些属性(不使用 `any`类型)

python - 如何调用从 Python 获取并返回自定义类型指针的 delphi 函数?

delphi - 尝试...除了...最后在德尔福有什么意义?

delphi - dwscript - 如何枚举所有可用类型?

c - 为什么 c 数组声明为变量名而不是类型