Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to list left of a decimal?

Posted on 2007-08-07
18
Medium Priority
?
298 Views
Last Modified: 2012-05-05
The following:
#val(listRest(variables.Width,".")) will list all variables right of a decimal.
6.625 = 625
100.625 = 625
I need somthing that will list left of the decimal
6.625 = 6
100.625 = 100
#left(Width,4)# will not work because variables to the left of "." are dynamic.
Any ideas?
0
Comment
Question by:dawes4000
  • 6
  • 5
  • 4
  • +1
18 Comments
 
LVL 14

Expert Comment

by:Scott Bennett
ID: 19650234
#listgetat(variables.Width,1,".")#
0
 
LVL 14

Expert Comment

by:Scott Bennett
ID: 19650239
that will treat the variable as a decimal delimited list and give you the first item in the list (everything left of the decimal)
0
 
LVL 15

Expert Comment

by:danrosenthal
ID: 19650578
Same concept, but less code:
#listFirst(variables.Width,".")#
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 

Author Comment

by:dawes4000
ID: 19650589
Yes, #listFirst(variables.Width,".")#
works just as #listgetat(variables.Width,1,".")# worked
Same results anyway.

What would be the best senario for "listgetat", and
what is the purpose of "1" as a position?
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 19650599

 If you're dealing with numbers, just use int()  
 That way, if there's no decimal, you will still get the value you want.

  int(6.2345)

  int(123)

0
 

Author Comment

by:dawes4000
ID: 19650693
The app I'm creating will need the decimal. I will need to convert
.625 to 5/8 and .25 to 1/4 and so forth. The client needs the fractions
for display purposes. So I'm writing a series of if elseif statements that
convert the decimal into the fraction.

<cfoutput>#val(listFirst(variables.HT,"."))#</cfoutput>
  <cfif #val(listRest(variables.HT,"."))# EQ 375>            
    <sup>3/8"</sup>
  <cfelseif #val(listRest(variables.HT,"."))# EQ 625>
    <sup>5/8"</sup>
  <cfelseif #val(listRest(variables.HT,"."))# EQ 875>
    <sup>7/8"</sup>
  <cfelseif #val(listRest(variables.HT,"."))# EQ 0625>
    <sup>1/16"</sup>
  <cfelseif #val(listRest(variables.HT,"."))# EQ 5625>
    <sup>9/16"</sup>
  <cfelse>
    <cfoutput>#val(listRest(variables.HT,"."))#</cfoutput>
</cfif>
0
 
LVL 15

Expert Comment

by:danrosenthal
ID: 19650696
ListGetAt lets you specify the First, Second, Third, etc. element of a list.
ListFirst gets the first element.

ListGetAt is useful if you want the 3rd element in a list as an example:
ListGetAt("100.200.300.400",".",3) = 300
0
 
LVL 15

Expert Comment

by:danrosenthal
ID: 19650724
You should use CFSWITCH/CFCASE for more readable code:

<cfoutput>#val(listFirst(variables.HT,"."))#</cfoutput>
<CFSWITCH expression="#val(listRest(variables.HT,"."))#">
    <CFCASE value="375">
        <sup>3/8"</sup>
    </CFCASE>
    <CFCASE value="625">
        <sup>5/8"</sup>
    </CFCASE>
    <CFCASE value="875">
        <sup>7/8"</sup>
    </CFCASE>
    <CFDEFAULTCASE>
    <cfoutput>#val(listRest(variables.HT,"."))#</cfoutput>
    </CFDEFAULTCASE>
</CFSWITCH>
 
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 19650925

<cfset variables.decimal = "6.125">


this is the number to the left of the decimal :   #int(variables.decimal)#

This is the number to the right of the decimal, even if there is NO decimal in the number

  #variables.decimal - int(variables.decimal)#

Then you can convert this second value to a fraction.


If you try the other posts method using list functions with the number 6
you will not get the results you seek

0
 
LVL 14

Expert Comment

by:Scott Bennett
ID: 19655806
Here is a UDF that will tak your number and write it out as a fraction:

<cfscript>
function getGCD(a, b) {
  var remainder = 0;
 
  while (b NEQ 0) {
    remainder = a MOD b;
    a = b;
    b = remainder;
  }

  return abs(a);
}

function toFraction(number, mixedNum) {
 
  // variable declarations
  var posNeg    = 1;
  var wholeNum  = "";
  var numerator  = "";
  var denominator  = "";
  var fraction   = "";

  // make sure the decimal is not in the first or last position
  if ((left(trim(number), 1)) IS '.') number = "0" & number;
  if ((right(trim(number), 1)) IS '.') number = number & "0";

  // return if invalid number (return 0) or an integer (return integer/1)
  if (NOT isNumeric(number)) return 0;
  if ((NOT find('.', number)) OR (NOT listLast(number, '.'))) {
    fraction = int(number) & "/1";
    return fraction;
  }
 
  if (number LT 0) {
    posNeg = -1;
  }

  wholeNum  = abs(val(listFirst(number, ".")));
  numerator  = abs(val(listLast(number, ".")));
  denominator  = "1";
 
  for (i=1; i LTE len(numerator); i=i+1) {
    denominator = denominator & "0";
  }

  gcd = getGCD(numerator, denominator, number);
 
  numerator  = numerator/gcd;
  denominator  = denominator/gcd;
 
  if (mixedNum AND wholeNum) {
    fraction = posNeg * wholeNum & " <span
style=""font-size:8px;""><sup>" & abs(numerator) & "</sup>/<sub>" &
denominator & "</sub></span>";
  } else {
    numerator = wholeNum * denominator + numerator;
    fraction = posNeg * numerator & "/" & denominator;
  }
  return fraction;
}
</cfscript>

<cfoutput>#toFraction(6.25,true)#</cfoutput>
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 19656175
sweet piece of code.    

That's the best solution as you don't have to hard code every possible fraction.


 The only small thing I see with it is that if you pass it "6" it will return 6/1  which I suspect is not desirable.  I believe this can be easily adjusted (if desired) using the second parameter ...

if ((NOT find('.', number)) OR (NOT listLast(number, '.'))) {
    if (mixedNum) return int(number)
   else {
       fraction = int(number) & "/1";
       return fraction;
       }
  }
0
 
LVL 14

Accepted Solution

by:
Scott Bennett earned 2000 total points
ID: 19656439
here it is again, fixed as per gdemaria's comment:

<cfscript>
function getGCD(a, b) {
  var remainder = 0;
 
  while (b NEQ 0) {
    remainder = a MOD b;
    a = b;
    b = remainder;
  }

  return abs(a);
}

function toFraction(number, mixedNum) {
 
  // variable declarations
  var posNeg    = 1;
  var wholeNum  = "";
  var numerator  = "";
  var denominator  = "";
  var fraction   = "";

  // make sure the decimal is not in the first or last position
  if ((left(trim(number), 1)) IS '.') number = "0" & number;
  if ((right(trim(number), 1)) IS '.') number = number & "0";

  // return if invalid number (return 0) or an integer (return integer/1)
  if (NOT isNumeric(number)) return 0;
  if ((NOT find('.', number)) OR (NOT listLast(number, '.'))) {
    if (mixedNum) return int(number);
   else {
       fraction = int(number) & "/1";
       return fraction;
       }

  }

 
  if (number LT 0) {
    posNeg = -1;
  }

  wholeNum  = abs(val(listFirst(number, ".")));
  numerator  = abs(val(listLast(number, ".")));
  denominator  = "1";
 
  for (i=1; i LTE len(numerator); i=i+1) {
    denominator = denominator & "0";
  }

  gcd = getGCD(numerator, denominator, number);
 
  numerator  = numerator/gcd;
  denominator  = denominator/gcd;
 
  if (mixedNum AND wholeNum) {
    fraction = posNeg * wholeNum & " <span
style=""font-size:8px;""><sup>" & abs(numerator) & "</sup>/<sub>" &
denominator & "</sub></span>";
  } else {
    numerator = wholeNum * denominator + numerator;
    fraction = posNeg * numerator & "/" & denominator;
  }
  return fraction;
}
</cfscript>

<cfoutput>#toFraction(6,true)#</cfoutput>
0
 

Author Comment

by:dawes4000
ID: 19740715
SBennet,
that is a nice piece of code. Are you aware of any scripting errors?
The code appears to have conditions for invalid integers. I am supplying
the fuction with the numbers (no user interface) so no risk of non-numerics.
I've created a struct that takes 3 inputs and outputs 30 variables. The call to
"toFraction" works superb!
<cfinvoke component="sitecfc" method="CalcMeasurements" Width="#Width#" Length="#Length#" Height="#Height#" returnvariable="calc">
THEN
<cfoutput>#toFraction(calc.HT,true)#</cfoutput>
More than I was looking for.
0
 
LVL 14

Expert Comment

by:Scott Bennett
ID: 19740824
It is a UDF I have had in my library for a while and I haven't had any issues with it. if you ever have any problems with it then let me know and I will debug them.
0
 

Author Comment

by:dawes4000
ID: 19741007
Thanks,
curious, I have been coding for over a year now, in coldfusion, and have some scripts, and methods of doing things that I would like to organize. This is off topic but what have you found best to do this? Wondering if gdemaria has some input into this as well?
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 19741215

My method of organizing isn't too exciting.   The "snippets" tab of coldfusion studio and I believe dreamweaver is where I keep smaller coding blocks.   Information that is more involved I simply organize into folders grouped by technology : javascript, coldfusion, sql server, etc.

Of course, any code that is shared and can be utilized from my applications can be found in a folder hierarchy I called "library".   Within the library folder, I have subfolders cfc, javascript, images, etc where I keep globably shared components.
0
 
LVL 14

Expert Comment

by:Scott Bennett
ID: 19741381
My system is very much like gdemari's I usually keep UDF's like this I keep in a folder on my laptop called UDFs and that has subfolders for UDF categories like Mathematics, Strings, Arrays, Dates,etc etc

The categories I use are very similar to how www.cflib.org oragnizes the UDF's they have available to the public. And that is a great place to find UDF's by the way if you're like me and don't like reinventing the wheel. Some of them aren't perfect but they are open source so you can modify them to your specific needs.

I do the same thing with custom tags and cfc's and javascript functions that I have in my library.  But for certain custom tags I use often I wil create a .vtm file for homesite+ (yes I still use homesite+ because I am resistant to change and dreamweaver is just a little bit too different for me)

And I also use snippets for little bits of code I use frequently but those are usually for things that aren't self contained modules like UDFs, javascript functions,CFCs, and custom tags.
0
 

Author Comment

by:dawes4000
ID: 19757224
Thank you all for your assistance.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question