Topic: Conversion


donboe    -- 01-31-2009 @ 6:53 AM
  I have a conversion table

Fractional to Decimal Conversion

Decimal to Fractional Conversion

I have the first conversion working! When people type in 1 "/" 2 in two sepperate text fields a third text field shows 1.50 using this code:
<cfset fractionResult = form.fraction1 / form.fraction2 + 1>
But somehow I can't get it work the other way arround:I tried:
<cfset decimalResult = Form.dec_box - 1 & "/1">
But then when I enter the 1.50 in the text field it gives the following result: 0.5/1 where it should give 1/2 like in the first example! With round numbers though (1, 2, 3, 4 et) it works fine. It has to do with the decimals

Where am I going wrong. Have to admit though that math was never my strongest point Confused

This message was edited by donboe on 1-31-09 @ 7:02 AM


RafaelQ    -- 01-31-2009 @ 4:38 PM
  There's a bit more to converting a decimal into a fraction. For a simple refresher http://www.mathsisfun.com/converting-decimals-fractions.html

As you can see, it takes a little more effort to convert a decimal into a fraction. Fortunately there is a custom tag that'll do this for you.

Usage Instructions:
http://blog.zog.org/2008/10/13745.html

Download:
http://store2.adobe.com/cfusion/exchange/index.cfm?event=extensionDetail&extid=1001435#


donboe    -- 02-02-2009 @ 7:19 AM
  Hi RafaelQ. Thank yo for the links. It maybe sound stupid, but how do I integrate this UDF in a form? What I would like to accomplish is to add a decimal like 3.95 in formfield and when I submit the form the fraction result should show up in a second text field.


RafaelQ    -- 02-02-2009 @ 12:44 PM
  This should get you started.
<cfsetting enablecfoutputonly="yes">
<cfparam name="resultval" default="">

<cfif isDefined("FORM.convertthis")>
     <cfif isNumeric(FORM.convertthis)>
    <cf_fraction number="#FORM.convertthis#" show="no" precision="8">
         <cfif num eq 0  AND denom eq 1>
         <cfset resultval = "#intpart#">
        <cfelseif intpart eq 0>
        <cfset resultval = "#num#" & "/" & "#denom#">
        <cfelse>
         <cfset resultval = "#intpart#" & " " & "#num#" & "/" & "#denom#">
        </cfif>
    <cfelse>
    Invalid value submitted. Try again this time with a number.
    </cfif>
</cfif>

<cfoutput>
<form method="post" target="_self" >
Enter the decimal you would like to convert to a fraction:
<input name="convertthis" type="text" />
<input type="submit" value="Submit" />
<br />Result:
<input name="result" type="text" value="#resultval#" />
</form>
</cfoutput>


This message was edited by RafaelQ on 2-2-09 @ 1:16 PM


donboe    -- 02-03-2009 @ 2:32 AM
  Hi Rafael. Thank you so much for the responce. I tried everything, but get keeping the message The website cannot display the page;, which is kind of frustratin because I can't make up if there's anything i'm doing wrong. For testing purpose I used your example as you wrote it, but nothing so far?

Thank you in advance




donboe    -- 02-03-2009 @ 2:34 AM
  Don't know what happend, but somehow this was the same post as the one from 2 minutes earlier. Sorry for that!

This message was edited by donboe on 2-3-09 @ 3:25 AM


RafaelQ    -- 02-03-2009 @ 5:24 PM
  Not sure what to tell you other than confirm that the URL you've entered matches the location of your test page.

IE, http://www.yourdomain.com/dev/fractiontest.cfm = \\wwwroot\dev\fractiontest.cfm

Also, make sure the fraction.cfm file is in the same directory as your form.

This may also help:
http://www.houseoffusion.com/groups/cf-talk/thread.cfm/threadid:58173

This message was edited by RafaelQ on 2-3-09 @ 5:35 PM


cfSearchin    -- 02-03-2009 @ 10:04 PM
 
quote:

but get keeping the message The website cannot display the page;, which is kind of frustratin because I can't make up if there's anything i'm doing wrong.


If you are using Internet Explorer, verify you disabled the "Show Friendly HTTP Error Messages" option. That can sometimes mask error messages.

http://technet.microsoft.com/en-us/library/cc778248.aspx


donboe    -- 02-04-2009 @ 1:27 AM
  That made absolute sense (about the Show Friendly HTTP Error Messages) Blush  Now i get the error message that the Variable NUM is undefined. For good order here the entire code:
<cfsetting enablecfoutputonly="yes">
<cfparam name="resultval" default="">
<cfif isDefined("FORM.convertThis")>    
<cfif isNumeric(FORM.convertThis)>    
<cf_fraction number="Form.convertThis" precision="8">        
<cfif num eq 0  AND denom eq 1>        
<cfset resultval = "#intpart#">        
<cfelseif intpart eq 0>        
<cfset resultval = "#num#" & "/" & "#denom#">        
<cfelse>        
<cfset resultval = "#intpart#" & " " & "#num#" & "/" & "#denom#">        
</cfif>    
<cfelse>    Invalid value submitted. Try again this time with a number.    
</cfif></cfif>
<cfoutput>
<form action="#cgi.script_name#" method="post" name="convertForm" target="_self" id="convertForm">
<label>Enter Decimal:
<input name="convertThis" type="text" id="convertThis" />
</label>
<p>
<label>Result:
<input name="result" type="text" id="result" value="#resultval#" />
</label>
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</cfoutput>





RafaelQ    -- 02-04-2009 @ 11:35 AM
  http://livedocs.adobe.com/coldfusion/8/htmldocs/reuseCode_2.html

**Specifically**

You must store custom tag pages in any one of the following:

    * The same directory as the calling page
    * The cfusion\CustomTags directory
    * A subdirectory of the cfusion\CustomTags directory
    * A directory that you specify in the ColdFusion Administrator

This message was edited by RafaelQ on 2-4-09 @ 11:35 AM


donboe    -- 02-06-2009 @ 2:10 AM
  I got it working, finally  Smile . I owe a lot of thanks to rafael. I don't know why it wasn't working in the first place though. Everything suggested by rafael (same directory etc) was done. Anyway, this morning I deceided to start from scratch and it's working fine.

One note: the result of this UDF gives a decimal fraction (7 1/2 etc) where I needed a common fraction (13/2), so for anyone who might need this in the future find the  script below:

<cfif isDefined( "Form.convert_decimal" ) >
<cfif isDefined( "Form.convertVal" ) >
<cf_fraction number="#Form.convertVal#" show="no" precision="8">      
<cfif num eq 0  AND denom eq 1>        
<cfset resultval = "#intpart#">        
<cfelseif intpart eq 1>        
<cfset resultval = "#num#" & "/" & "#denom#">        
<cfelse>
<!--- Replace This --->
<cfset resultval = "#intpart#" & " " & "#num#" & "/" & "#denom#">
<!--- By this for common fraction --->        
<cfset resultval = (("#intpart#" * "#denom#") + "#num#" - "#denom#")  & "/" & "#denom#">          
</cfif>
<cfelse>    
Invalid value submitted. Try again this time with a number.    
</cfif>
</cfif>






RafaelQ    -- 02-06-2009 @ 10:08 AM
  Glad to see you got it working  Smile


EasyCFM.COM ColdFusion Forums : http://www.easycfm.com/forums
Topic: http://www.easycfm.com/forums/viewmessages.cfm?Forum=12&Topic=14038