在Delphi中开发的服务中的浮点转换

时间:2017-08-19 16:29:10

标签: delphi

我在Delphi中使用DataSnap和Tethering开发了一个服务,它将信息发送给连接的客户端。现在,当你使用函数" FormatFloat(' $,0。###',field)"将它们转换为字符串时,某些字段是浮点数。它给了我另一种格式,即它不会以我在Windows中配置的格式发送给我,"。"对于数千个分隔符和","小数,但相反。我希望15674.45是15.647,45美元而不是15,647.45美元。但我不想强制格式化。

procedure TServerContainerSGV40.tapServicioResourceReceived(const Sender: TObject; const AResource: TRemoteResource);
var
  identifier, hint, cadena: string;
  ID_PRODUCTO: Integer;
  codigo, descripcion: string;
  ppp, stock, precio_venta: Real;
begin
  if AResource.ResType = TRemoteResourceType.Data then
  begin
    identifier := Copy(AResource.Hint, 1, Pos('}', AResource.Hint));
    hint := AResource.Hint.Replace(identifier, '');
    cadena := AResource.Value.AsString;
    if cadena = 'Get IP' then EnviarCadena(AResource.Hint, 'Envío IP', GetLocalIP);
    if hint = 'Datos Producto' then
    begin
      if cadena.Length > 0 then
      begin
        with usGetDatosProducto do
        begin
          ParamByName('CODIGO').AsString := cadena;
          Execute;
          ID_PRODUCTO := ParamByName('ID_PRODUCTO').AsInteger;
          codigo := ParamByName('CODIGO').AsString;
          descripcion := ParamByName('DESCRIPCION').AsString;
          ppp := ParamByName('PPP').AsFloat;
          stock := ParamByName('STOCK').AsFloat;
          precio_venta := ParamByName('PRECIO_VENTA').AsFloat;
        end;
        if ID_PRODUCTO > 0 then
        begin
          cadena := Format('%s;%s;;PRECIO:'#9'%s;P.P.P.:'#9'%s;STOCK:'#9'%s', [
            codigo, descripcion, FormatFloat('$ ,0', precio_venta),
            FormatFloat('$ ,0.##', ppp), FormatFloat(',0.###', stock)
          ]);
          EnviarCadena(identifier, 'Envío Datos Producto', cadena);
        end
        else
          EnviarCadena(identifier, 'Mostrar Mensaje', 'Código de Producto No Existe');
      end;
    end;
  end;
end;

2 个答案:

答案 0 :(得分:3)

默认情况下,FormatFloat()使用全局SysUtils.ThousandsSeparatorSysUtils.DecimalSeparator变量, 在程序启动时从操作系统设置初始化

FormatFloat('$#,##0.00', field);

如果您想强制使用特定格式而不考虑操作系统设置,请使用以FormatFloat()作为输入的TFormatSettings的重载版本:

var
  fmt: TFormatSettings;

fmt := TFormatSettings.Create;
fmt.ThousandsSeparator := '.';
fmt.DecimalSeparator := ',';
FormatFloat('$#,##0.00', field, fmt);

答案 1 :(得分:2)

在D2009的Delphi版本中(至少),您可以specify format settings进行给定操作,并通过Windows默认设置或修改所需的格式化字段来初始化这些设置。

function FormatFloat(const Format: string; Value: Extended): string; overload;
function FormatFloat(const Format: string; Value: Extended; 
                     const FormatSettings: TFormatSettings): string; overload;

我想知道 - 只用Format函数形成所有字符串是不可能的吗?