Re: Converting decimal number to carpenter's fractions

new topic     » goto parent     » topic index » view thread      » older message » newer message
K_D_R said...

Does anyone have, or can recommend a routine to convert decimals to carpenters inch fractions: 32nds, 16ths, 8ths, 4ths, halfs?

 
 
constant mm_inch = 1/25.4 
 
function carp( atom x, atom conv = 1) 
    integer numerator 
    integer denominator 
    integer units 
     
    x *= conv 
    if x > 1 then 
        units = floor(x) 
        x -= units 
    else 
        units = 0 
    end if   
    numerator = floor( x * 64 + 0.5) 
    denominator = 64 
    while denominator > 1 do 
        if remainder(numerator,2) = 1 then 
            exit 
        end if 
     
        numerator /= 2 
        denominator /= 2 
    end while 
 
    return {units,numerator,denominator} 
 
end function 
 
? carp(0.625) --> {0,5,8}  ie. five eigths 
 
? carp(2.314) --> {2,5,16}  ie. two and five sixteenths 
 
? carp(38, mm_inch) --> {1,1,2}  ie. 38mm -> 1 1/2 inch 
new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu