I have an application which will use measurements, specifically down to 1/16 of an inch. I would really like a convenient way for an end user to enter a value INCLUDING a fractional part, for example, 3 7/16. I realize that I can require the user to just enter decimal values (i.e. 3.1875), but I would really like a better way. Does anyone know of a drop down or spin control that makes this easy to enter? (ideally a DB version of the control.)
-
Get the user to enter text and then parse it. GUI controls would be horrid to use. – David Heffernan Aug 06 '13 at 14:15
-
or... if the only fractions you allow can be expressed as multiples of 1/16th then you only need to prompt your users for the numerator. A drop down list or spin control would be ok. You could allow for a greater range of pre-set fractions by using one control for the numerator and one for the denominator, either side by side with a "/" in between, or one above the other with a line drawn on the canvas between them (perhaps a rectangular TShape with height of 1 or 2 pixels). – Sam Aug 07 '13 at 04:14
-
What do you want to do with these fractions? I think you should consider permitting the user to enter free-form expressions like `3/4"` and then be able to hold these values in memory using a class that you design that can store both the user's input and then manipulate the values. I would call it `TDimension` and it would have both the original string input value and its decimal value inside as fields. – Warren P Aug 07 '13 at 23:36
2 Answers
You can do simply
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;
This will accept input of the form examplified by 3/7
and -4 / 91.5
.
To allow an integer part, add
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;
This will in addition accept input of the form examplified by 1 1/2
.

- 105,602
- 8
- 282
- 384
-
2And extend this with the option to enter the integer number in front (you can detect this by the presence of a space char) - just typing the complete number in one TEdit is the easiest way for the user *and* allows them to use decimals in the same control, if they'd rather type 3.5 than 3 1/2 – Jan Doggen Aug 06 '13 at 14:24
-
I think I might be inclined to use the JCL expression evaluator here. With a tweak as suggested by Jan. – David Heffernan Aug 06 '13 at 14:24
-
The idea of using a true expression evaluator is good. Then you can input things like `4.5 + (10.2 + 3.83)*3.67`. – Andreas Rejbrand Aug 06 '13 at 14:38
-
What we really need is a fractions class in the Math unit, which hasn't seem much updates since the days of Turbo Pascal. :-( But we'd also need operator overloading of classes for that to be useful, and for that we'll need automatic memory management. Hopefully these things show up in XE5. – alcalde Aug 07 '13 at 00:34
-
@alcalde Absolutely not. Numbers are best represented as values rather than references and so operator overloading with records which you already have suffices. – David Heffernan Aug 07 '13 at 06:49
I happen to have written a control many years ago and am currently finishing up an update to it that includes metric conversion as well. It allows the user and developer to toggle between ft in, dec in, dec ft, mm, cm, m just with a right click or setting the mode in design time. It also handles rounding to the nearest 16th, 32nd or what ever you need for metric. Check it out on most of the delphi component sites like torry.net or my old site at http://www.enhancedtechsolutions.com/delphi/ I hope to have the new version done in a day or 2 and published.
Forgot to mention the most important part: It is call TMaskFtInch

- 122
- 10
-
-
-
Nope, but as David stated it wouldn't be too difficult for you to manage the i/o via code instead of just a control link. The download includes a demo exe so that you can check out the current functionality with out having to install anything. – Gary Mueller Aug 06 '13 at 21:16
-