Separator tysięczny

0

Witam

Znalalzem taki kodzik w necie

function TGlowna.SeparateInt(Value: Int64; Separator: Char): String;
var Negative: Boolean;
begin
  Negative := Value < 0;
  Value := Abs(Value);
  Result := IntToStr(Value mod 1000);
  Value := Value div 1000;
  while Value mod 1000 <> 0 do begin
    Result := IntToStr(Value mod 1000) + Separator + Result;
    Value := Value div 1000;
  end;
  if Negative then
    Result := '-' + Result;
end;

wszytko ladnie pieknie tylko w wywapdku kiedy mam ciag 111.004.111

to wyswietla mi zamiast 111.004.111 to 111.4.111

Jak to naprawić???

0

a nie łatwiej np tak:

function TGlowna.SeparateInt(Value: Int64; Separator: Char): String;
var s: string; i: integer;
begin
  s := IntToStr(Value);
  Result := '';
  for i := Length(Result) downto 1 do begin
    Result := s[i] + Result;
    if (((Length(Result) - i) mod 3) = 0) and (not s[i] = '-') then
      Result := Result + Separator;
  end;
end;

to taki głupi przykład na szybko napisany - nie wiadomo czy działający ;P no ale ogólnie już operując na stringach

0

Problem zapewne leży w tej linii:

Value := Value div 1000;

I wszystkie 00x zamienia na x, a 0y na y więc zrób to na stringu od razu (jadąc od tyłu co trzy).

0

zamienić całość na string a potem co 3 znaki (licząc od końca) powtykać kropki

0

Ciekawe po co sobie utrudniac zycie??

a moze to tak zrobic:

function zamien(i : int64;sep : char) : string;
var d : TFormatSettings;
begin
  d.ThousandSeparator := sep;
  Result := FormatFloat('#,##0',i,d);
end;

1 użytkowników online, w tym zalogowanych: 0, gości: 1