use*_*073 2 delphi delphi-2010
我有一个应用程序将使用测量,特别是低至1/16英寸.我真的希望最终用户输入包含小数部分的值的便捷方式,例如3 7/16.我意识到我可以要求用户输入十进制值(即3.1875),但我真的想要一个更好的方法.有没有人知道下拉或旋转控制使这容易进入?(理想情况下是控件的DB版本.)
你可以做的很简单
function FractionToFloat(const S: string): real;
var
BarPos: integer;
numStr, denomStr: string;
num, denom: real;
begin
BarPos := Pos('/', S);
if BarPos = 0 then
Exit(StrToFloat(S));
numStr := Trim(Copy(S, 1, BarPos - 1));
denomStr := Trim(Copy(S, BarPos + 1, Length(S)));
num := StrToFloat(numStr);
denom := StrToFloat(denomStr);
result := num/denom;
end;
Run Code Online (Sandbox Code Playgroud)
这将接受由3/7和举例说明的形式的输入-4 / 91.5.
要允许整数部分,请添加
function FullFractionToFloat(S: string): real;
var
SpPos: integer;
intStr: string;
frStr: string;
int: real;
fr: real;
begin
S := Trim(S);
SpPos := Pos(' ', S);
if SpPos = 0 then
Exit(FractionToFloat(S));
intStr := Trim(Copy(S, 1, SpPos - 1));
frStr := Trim(Copy(S, SpPos + 1, Length(S)));
int := StrToFloat(intStr);
fr := FractionToFloat(frStr);
result := int + fr;
end;
Run Code Online (Sandbox Code Playgroud)
这将另外接受由examplified表示的输入1 1/2.