program Project1;
{$IFDEF MSWINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
uses
SysUtils;
type
TNumberType = (ntInvalid, ntFloat, ntInteger);
TNumber = record
FType: TNumberType;
FInteger: Integer;
FFloat: Double;
FString: string;
end;
function TryToNumber(const AValue: string; out AOut: TNumber): Boolean;
var
i: Double;
s: string;
begin
Result := False;
AOut.FType := ntInvalid;
AOut.FFloat := 0.0;
AOut.FInteger := 0;
AOut.FString := '0';
if TryStrToFloat(AValue, i) then
begin
if Frac(i) = 0.0 then
begin
AOut.FType := ntInteger;
AOut.FInteger := Trunc(i);
AOut.FString := IntToStr(Trunc(i));
end
else
begin
AOut.FType := ntFloat;
AOut.FFloat := i;
AOut.FString := FloatToStrF(i, ffFixed, 16, 2);
end;
Result := True;
Exit;
end;
if ((Pos('.', AValue) <> 0) and (FormatSettings.DecimalSeparator <> '.')) then
begin
s := AValue.Replace('.', ',', [rfReplaceAll]);
if TryStrToFloat(s, i) then
begin
if Frac(i) = 0.0 then
begin
AOut.FType := ntInteger;
AOut.FInteger := Trunc(i);
AOut.FString := IntToStr(Trunc(i));
end
else
begin
AOut.FType := ntFloat;
AOut.FFloat := i;
AOut.FString := FloatToStrF(i, ffFixed, 16, 2);
end;
Result := True;
Exit;
end;
end;
if ((Pos(',', AValue) <> 0) and (FormatSettings.DecimalSeparator <> ',')) then
begin
s := AValue.Replace(',', '.', [rfReplaceAll]);
if TryStrToFloat(s, i) then
begin
if Frac(i) = 0.0 then
begin
AOut.FType := ntInteger;
AOut.FInteger := Trunc(i);
AOut.FString := IntToStr(Trunc(i));
end
else
begin
AOut.FType := ntFloat;
AOut.FFloat := i;
AOut.FString := FloatToStrF(i, ffFixed, 16, 2);
end;
Result := True;
Exit;
end;
end;
end;
function CountDecimals(Value: string): string;
var
s,s2: string;
p,nInt,nlen: integer;
begin
nlen := Length(value);
p := pos(FormatSettings.DecimalSeparator, Value);
if p > 0 then
begin
s2 := copy(Value, p+1,nlen );
nInt := StrToInt(s2);
if nInt > 0 then
result := Value
else
result := copy(Value, 1, p-1);;
end;
end;
var
s: string;
LNum: TNumber;
begin
WriteLn('');
WriteLn('KodeZwerg:');
WriteLn('');
s := '12.34';
if TryToNumber(s, LNum) then
WriteLn(s, ' -> ', LNum.FString)
else
WriteLn(s, ' could not be converted!');
s := '56,78';
if TryToNumber(s, LNum) then
WriteLn(s, ' -> ', LNum.FString)
else
WriteLn(s, ' could not be converted!');
s := '9.00';
if TryToNumber(s, LNum) then
WriteLn(s, ' -> ', LNum.FString)
else
WriteLn(s, ' could not be converted!');
s := '99,00';
if TryToNumber(s, LNum) then
WriteLn(s, ' -> ', LNum.FString)
else
WriteLn(s, ' could not be converted!');
s := 'abc';
if TryToNumber(s, LNum) then
WriteLn(s, ' -> ', LNum.FString)
else
WriteLn(s, ' could not be converted!');
WriteLn('');
WriteLn('Team Prakash:');
WriteLn('');
s := '12.34';
WriteLn(s, ' -> ', CountDecimals(s));
s := '56,78';
WriteLn(s, ' -> ', CountDecimals(s));
s := '9.00';
WriteLn(s, ' -> ', CountDecimals(s));
s := '99,00';
WriteLn(s, ' -> ', CountDecimals(s));
s := 'abc';
WriteLn(s, ' -> ', CountDecimals(s));
{$IFDEF MSWINDOWS}ReadLn; {$ENDIF}
end.