I am writing a little conversion piece in ColdFusion CFSCRIPT.
I need to convert a weight in pounds to pounds and ounces.
So, 3.1565 needs to become 3 pounds and 3 ounces. 1.512 will become 1 pound and 9 ounces (round up the ounces).
0.25 will become 0 pounds and 4 ounces.
My thought is to take the total weight in pounds and multiply it by sixteen, which will give me the total ounces. Then I'll need to extract the even pounds by dividing by sixteen and the remainder will be the ounces. I really don't know how to do this accurately and with efficient code.
<cfscript>
MyPounds = 0;
MyOunces = 0;
ThisPounds = 2.12345;
MyOunces = (ThisPounds * 16);
// EXTRACT THE NUMBER OF POUNDS
// REMAINDER IS OUNCES - ROUND UP
}
</cfscript>
Something like this (not extensively tested).
EDIT: If the input can be negative, use the abs() value for calculations
<cfset theInput = 0.25>
<!--- round down to get total pounds --->
<cfset lbs = int(theInput)>
<!--- extract remainder. multiply by 16 and round up --->
<cfset ounces = ceiling((theInput - lbs) * 16)>
<cfoutput>#lbs# pounds #ounces# ounces</cfoutput>
Integer division and Modulus should give you the values you need.
<cfscript>
MyPounds = 0;
MyOunces = 0;
ThisPounds = 2.12345;
MyOunces = (ThisPounds * 16);
// EXTRACT THE NUMBER OF POUNDS
weightInPounds = MyOunces \ 16;
// REMAINDER IS OUNCES - ROUND UP
remainderOunces = ceiling(MyOunces MOD 16);
</cfscript>
This should do it:
<cffunction name="PoundConverter" returntype="string">
<cfargument name="Pounds" type="numeric" required="true" hint="" />
<cfset var TotalPounds = Fix(Arguments.Pounds) />
<cfset var TotalOunces = Ceiling((Arguments.Pounds - TotalPounds) * 16) />
<cfreturn TotalPounds & " pounds and " & TotalOunces & " ounces" />
</cffunction>
<cfoutput>
#PoundConverter(3.1565)#<br />
#PoundConverter(1.512)#
</cfoutput>
You pretty much have what you need. To extract the number of pounds, divide by 16. The remainder (the "mod") is the ounces.
<cfscript>
function poundsandounces( initvalue ) {
var rawvalue = val( initvalue ) * 16;
var lbs = int( rawvalue / 16 );
var oz = ceiling( rawvalue % 16 );
return "#lbs# pounds #oz# ounces";
}
</cfscript>
<cfoutput>#poundsandounces( 0.25 )#</cfoutput>
Related
I have column with number of seconds and I'm trying to convert it to HH:mm:ss format.
All is working well if values are not empty or above 32767 since this is limitation of TIME function.
My question is: how can I still convert values above 32767 in HH:mm:ss format?
What I have so far is:
time_elapsed = IF(ISBLANK([time_in_sec]);"NaN";FORMAT(TIME(0;0;[time_in_sec]);"HH:mm:ss"))
But this not even checking because I don't know how to pass empty field as empty field and not Null or "Nan" or anything else when dealing with integer column.
For all other cases function FORMAT(TIME(0;0;[time_in_sec]);"HH:mm:ss") works well.
So 2 problems - how to convert numbers larger than 32767 to HH:mm:ss and how to just pass empty fields.
And in case of negative integer number it should return empty field as well.
1) It's possible that space character or another unprintable character may be present. In this case the value isn't considered BLANK. We need to see a sample of your data to tell exactly what's going on.
2) You can implement the formula, that converts seconds to the HH:MI:SS format, yourself, as follows:
// calculated column
hh_mi_ss =
VAR hr = FLOOR( query[column] / 3600, 1 ) // hours
VAR mn = FLOOR( MOD( query[column], 3600) / 60, 1) // minutes
VAR ss = FLOOR( MOD ( MOD( query[column], 3600) , 60 ), 1) // seconds
RETURN FORMAT( TIME(hr, mn,ss), "HH:mm:ss" )
FORMAT( DIVIDE( [DurationInSeconds] ) , 86400), "HH:mm:ss" )
This one-liner returns time part. It trims the day part if the DurationInSeconds is larger than 1 day (86400 seconds). If Duration is blank it returns blank.
Correct answer from #Nick Krasnov resolved my problem, I only needed to add IF function to regulate appearance of negative numbers, zeros and empty cells.
So I used:
hh_mi_ss = if([time_column]>0,(
VAR hr = FLOOR( [time_column] / 3600, 1) // hours
VAR mn = FLOOR( MOD( [time_column]; 3600) / 60, 1) // minutes
VAR ss = FLOOR( MOD ( MOD( [time_column], 3600) ,60 ), 1) // seconds
RETURN FORMAT( TIME(hr, mn,ss), "HH:mm:ss" ));
"Empty, 0 or negative value")
And in my locale I had to replace , with ; in argument of function.
When the program divides two user inputted numbers, then times that, the program returns 0 every time
correct = input("User, Input the amount of correct answers. :")
points_possible = input("User, Input the amount of points possible. :")
score = correct / points_possible
grade = score * 10
print grade
The expected output
if (1/2) * 10 = 5, but will output 0
If you are using Python 2.7 version, the input that would be taken from the console would always be in the form of strings. So, you'll need to convert it to integers.
#code in Python 3.5
correct = int(input("User, Input the amount of correct answers. :"))
points_possible = int(input("User, Input the amount of points possible. :"))
score = correct / points_possible
grade = score * 10
print(grade)
The reason why you're just getting 0 in Python 2 is that if integers are given then you'll get integer division only. If you want float division, you need to make sure there is a decimal point somewhere so that Python knows it doesn't have to truncate the value to an int.
#code in Python 2.7
correct = float(raw_input("User, Input the amount of correct answers. :"))
points_possible = float(raw_input("User, Input the amount of points possible. :"))
score = correct / points_possible
grade = score * 10.0
print grade
it's because python needs you to make it understand that you divide by a float: you can either add .0 at the end of the diviser or type 10/float(2)
I have a scenario where I need to round to the next 5kgs.
if the total weight is less than 5kgs, I need to round it to the next 5kgs.
if the item is just over 5kgs, for instance, 5.01 it needs to round up to 10kgs.
My math skills aren't the greatest, so Im hoping someone might be able to show me how to calculate it so it jumps to the next 5 kgs automatically
<cfset totalWeight = 5.01>
<cfset breakPoints = 5.00>
<cfset bagCost = 18.00>
<cfset totalWeight = ceiling(totalWeight)>
<cfif totalWeight LTE 5.00 >
<cfset totalBags = 1 >
<cfelse>
<cfset totalBags = totalWeight / breakpoints >
</cfif>
<cfset totalCost = totalBags * bagCost>
<cfoutput>
#totalweight#<br/>
#totalBags#
<hr/>
#totalCost#<br/>
<hr/>
</cfoutput>
To do something like this, the math logic is to divide the original value by 5, round it, and then multiply by 5. This guarantees you the next multiple of 5.
<CFSET RoundedValue = ceiling(totalWeight / 5) * 5>
Since you always want to round up, we use the CEILING function. If merely rounding to the nearest 5 (up or down), we would use the ROUND function instead.
I am trying to multiply the elements in the list so that they give me their total but with only using addition and subtraction. For example, a list of [1,3,6,8] will have the output 144. The code I have so far is:
numbers = [1,3,6,8]
def no_sign(numbers):
total = 0
answer = 0
for i in range(len(numbers)):
first_number = numbers[i]
print str(first_number) + ' pop'
for j in range(first_number):
#print first_number
answer = first_number + answer
print str(first_number) + ' firstnum'
print str(answer)+ " answeer "
total = total + answer
print str(total) + " total"
return total
print no_sign(numbers)
This only gives me 110, which isn't enough. Any suggestions?
Your code takes the square of each element and adds them up. Hence you are getting 1 + 9 + 36 + 64 = 110
Since you want to do same thing couple times, writing your multiplication(num1, num2) function yourself with only addition and using that when multiplying would be much better choice.
Multiplication of two numbers, as you know, is adding firstNumber to itself secondNumber of times. S you can write multiplication function like below and use it on a list.
def multiplication(num1, num2):
answer = 0
for i in range(num2):
answer += num1
return answer
numbers = [1,3,6,8]
def no_sign(numbers):
total = 1
for number in numbers:
total = multiplication(total, number)
return total
print no_sign(numbers)
Today I have written small program in ColdFusion to find the square root of a number. I found a strange issue. Not sure am I doing any silly mistake ?
<cfscript>
num = 16;
n = 0.0001;
i = 0;
for (i=0;i<num;i=i+n)
{
if((i*i)>num){
i = i - n;
break;
}
}
writedump(i);
</cfscript>
The output is 3.999 instead of 4 when num = 16. Where as when I set it to 36 its output is 6.
Not sure why I am getting 3.999 where as it should be 4.Any suggestion?
I altered your code to output the conditions you are seeing like so:
<cfscript>
num = 16.00;
n = 0.0001;
i = 0;
for (i=0;i<num;i=i+n)
{
writeoutput(i & " i * i = " & (i*i));
writeoutput('<br>');
if((i*i)>num){
i = i - n;
break;
}
}
writedump(i);
writeoutput('<br>');
</cfscript>
This outputs all the numbers and the multiples - a long list. The last 3 lines look like this:
3.9999 i * i = 15.99920001
4 i * i = 16
3.9999
That seems like expected behavior to me. Inside your break code you are reducing the value of i with this line:
i = i - n;
Is that not what you want?
Why do you want to do all the heavy lifting when we can use power of java as below:
<cfset testNumber = 20.50 />
<cfset mathObj = createObject( "java", "java.lang.Math" ) />
<cfset sqrtNumber = mathObj.sqrt(testNumber) />
<cfdump var="#sqrtNumber#"><cfabort>
Output:
4 -> 2
6 -> 2.44948974278
36- > 6
20.50 - > 4.52769256907
As you can see it works on all values including decimals. I hope this helps.
Note: Before passing values to the mathObj.sqrt, you should first check for negative values. You can not have sqrt of negative numbers.
Edit:
As Leigh pointed out there is already a function in CF, you can use that as follows:
<cfset testNumber = 36 />
<cfset sqrtNumber = sqr(testNumber) />
You will get output same as Java version code, only difference is that when you pass negative value in java code, you will get a gibberish value. With CF, you will get an error like this The 1 parameter of the Sqr function, which is now -122.0, must be a non-negative real number.
There is a problem in the if condition. When you use num=36 in that case at 6.001 the if condition gets evaluated and you get 6. But in the case of num=16 the if condition gets evaluated at 4.00 and you get 3.999.
But you may ask why at 4.00 it gets evaluated due to floating point numbers and floating point arithmetic.
Edit:
You can use Newton method to find square root of any +ve number.By this method you can achieve better performance over for loop that you had used.