Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[solved] decimal part to fraction converter


  • Please log in to reply
1 reply to this topic
smorgasboard
  • Members
  • 660 posts
  • Last active: Jan 14 2016 08:53 AM
  • Joined: 18 Jul 2012
i dedicate this script to the immense help given by the guy with the nick geekdude at the irc chat channel!

best wishes to GEEKDUDE!

there are a lot of message boxes to help, also it needs a lot of polishing but it delivers what it intends!

thanks ahk again!

InputBox, UserInput, CONVERTING DECIMAL PART IN FRACTION, ENTER ONLY THE PART AFTER DECIMAL, , 320, 120
userinput1 := UserInput
index = 1
K := Userinput / 10
;msgbox k is %K%
;msgbox index is %index%
loop
{
if k < %index%
{
;msgbox %a_index% digit number
q := a_index
;msgbox %q%
goto, label
return
}

else
{
index := index * 10
;msgbox %index%
}
}

label:
r := 10**q
msgbox the numerator is %r%
userinput2 := r
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;NUMERATOR FOUND, NOW SOLVING THE GCD PART;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
label2:
S := Mod(r, Userinput)
msgbox, %r% / %userinput% remainder is %S%
if S != 0
{
r := Userinput
Userinput := S
msgbox, %r% / %Userinput%
goto, label2

}
if S = 0
msgbox, the gcd is %userinput%
numerator := userinput1 / userinput
denominator := userinput2 / userinput
msgbox, %numerator% / %denominator%
finalN := Floor(Numerator)
finalD := Floor(denominator)
msgbox, FINAL ANSWER is %finalN%/%finalD%



o7 AHK!

GeekDude
  • Spam Officer
  • 391 posts
  • Last active: Oct 05 2015 08:13 PM
  • Joined: 23 Nov 2009
I've cleaned it up a bit, but I'm not quite sure how the logic in here goes so I can't optimize it.

Loop
{
    InputBox, UserInput, Decimal to fraction conversion, Enter a floating point number such as 3.14, , 320, 120
    if !UserInput
        ExitApp
    if UserInput is float
        break
}
msgbox, % Dec2Fract(UserInput)
return

Dec2Fract(Float)
{
    if Float is not float
        throw "Not a float"
    StringSplit, UI, Float, .
    Num := UI2

    r := 1
    While !(UI2 < r)
        r *= 10
    Den := r

    Loop
    {
        if ((S := Mod(r, UI2)) = 0)
            break
        r := UI2
        UI2 := S
    }

    return UI1 . (UI1 ? " " : "") . Floor(Num / UI2) . "/" . Floor(Den / UI2)
}