如何在Delphi中输入分数?

use*_*073 2 delphi delphi-2010

我有一个应用程序将使用测量,特别是低至1/16英寸.我真的希望最终用户输入包含小数部分的值的便捷方式,例如3 7/16.我意识到我可以要求用户输入十进制值(即3.1875),但我真的想要一个更好的方法.有没有人知道下拉或旋转控制使这容易进入?(理想情况下是控件的DB版本.)

And*_*and 7

你可以做的很简单

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.

  • 并使用选项在前面输入整数来扩展它(你可以通过空格char的存在来检测它) - 只需在一个TEdit中输入完整的数字对用户来说是最简单的方法*和*允许它们使用小数在相同的控制中,如果他们宁愿输入3.5比3 1/2 (2认同)