Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculating EXIF exposure time as a fraction (Delphi)
    primarykey
    data
    text
    <p>I am trying to display EXIF exposure time as a fraction of seconds but I am not getting expected result, probably a small mistake.</p> <p>I have found two routines that do the calculation but both bring diffrent and wrong result. </p> <p>The value I am having problem is: "0.0806451612903226" value is of type Extended.</p> <p>DecimalToFractStr give me: "16129/200000" as a result, DecToFrac gives me: "5/62" as a result</p> <p>Expected result should be "1/12" since everyone is getting it (what is the trick). When I right click image in explorer and view details I get this value. How do I get it in my application? Hope someone can help me here.</p> <p>Kind Regards Roy M Klever</p> <p>SOLVED! Ok after some testing I figured it out. Using the result from DecToFrac they simply integer divide it with the result from the left side. 5 div 5 = 1 / 62 div 5 = 12 so I am left with wanted result 1 / 12.</p> <p>Thank you for all your input.</p> <p>Kind Regards Roy M Klever</p> <pre><code>procedure DecimalToFract(value: double; AllowedDecimals: integer; var num, den: integer); var d, i: integer; ex: boolean; begin d := Trunc(power(10, AllowedDecimals)); num := Trunc(value * d); den := d; repeat ex := true; for i := 10 downto 2 do if ((num mod i) = 0) and ((den mod i) = 0) then begin num := num div i; den := den div i; ex := false; end; until ex; end; function DecimalToFractStr(value: double): string; var num, den: integer; begin DecimalToFract(value, 6, num, den); if den = 1 then result := inttostr(num) else result := inttostr(num) + '/' + inttostr(den); end; function Dec2Frac(f: Double): String; var df: Double; lUpperPart: Integer; lLowerPart: Integer; begin lUpperPart := 1; lLowerPart := 1; df := lUpperPart / lLowerPart; While (df &lt;&gt; f) do begin If (df &lt; f) Then lUpperPart := lUpperPart + 1 Else begin lLowerPart := lLowerPart + 1; lUpperPart := Trunc(f * lLowerPart); end; df := lUpperPart / lLowerPart; end; if lLowerPart = 1 then result := IntToStr(lUpperPart) else result := IntToStr(lUpperPart) + '/' + IntToStr(lLowerPart); end; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload