如何将整数标记为数千和数百?只是说我有一个整数12345678910,然后我想把它改成像12.345.678.910这样的货币值。我尝试以下代码,但它不是……
我现在知道,它如此简单。只是用
showMessage(formatFloat('#.###.00', strToFloat(original)));
但是感谢雷米,你打开了我的脑海。
此函数执行您指定的操作:
function FormatThousandsSeparators(Value: Int64): string; var Index: Integer; begin Result := IntToStr(Value); Index := Length(Result) - 3; while Index > 0 do begin Insert('.', Result, Index + 1); Dec(Index, 3); end; end;
请注意你的例子 12345678910 不适合32位有符号整数值,这就是我使用的原因 Int64 。
12345678910
Int64
此功能无法正确处理负值。例如,它返回 '-.999' 过了 -999 。这可以这样处理:
'-.999'
-999
function FormatThousandsSeparators(Value: Int64): string; var Index: Integer; Negative: Boolean; begin Negative := Value < 0; Result := IntToStr(Abs(Value)); Index := Length(Result) - 3; while Index > 0 do begin Insert('.', Result, Index + 1); Dec(Index, 3); end; if Negative then Result := '-' + Result; end;
在Delphi中有System.SysUtils.Format调用 http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.SysUtils.Format 。
这个电话了解 'm' 作为特定货币格式器的字符。 尝试这样的代码:
'm'
Value := 12345678910; FormattedStr := Format('Money = %m', [Value])
默认情况下 Format 将使用系统范围的格式设置,如果您必须覆盖默认系统设置,请参阅官方文档:
Format
转换由CurrencyString,CurrencyFormat控制, NegCurrFormat,ThousandSeparator,DecimalSeparator和 CurrencyDecimals全局变量或它们中的等价物 TFormatSettings数据结构。如果格式字符串包含 精度说明符,它会覆盖由给定的值 CurrencyDecimals全局变量或其TFormatSettings等价物。