Simple Calculations

Deploying to Windows? Utilizing VB Script execution? This is the place to ask Windows-specific questions.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
maverickalex
Posts: 108
Joined: Sun Mar 15, 2009 11:51 pm

Simple Calculations

Post by maverickalex » Mon Feb 28, 2011 8:08 am

if i have two fields with two different values and want to subtract one from the other, how do i do it.

using this i get the answer but not displayed in the right field.

Code: Select all

subtract the value of field "endfuel" from field "startfuel" 
  put the result into field "fuelused"
also i am subtracting two fields containing seconds and would like to divide the result by 60 to get hours/mins is it as simple as this?

Cheers
Alex

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Re: Simple Calculations

Post by BvG » Mon Feb 28, 2011 9:59 am

when you use subtract, then LC subtracts the first number from the last number, so the last number will be replaced by the result of the calculation. Same goes for divide, multiply, add, etc.

For example:

Code: Select all

put 5 into oneNumber
put 2 into otherNumber
subtract otherNumber from oneNumber
--now oneNumber = 3 and otherNumber = 2
you can instead use the calculation operators:

Code: Select all

put field 1 - field 2 / 60 into field 3
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

maverickalex
Posts: 108
Joined: Sun Mar 15, 2009 11:51 pm

Re: Simple Calculations

Post by maverickalex » Mon Feb 28, 2011 1:24 pm

Thanks, that worked a treat, just need to figure out how to retrieve the info whilst the .txt file is being accessed by the simulator.

maverickalex
Posts: 108
Joined: Sun Mar 15, 2009 11:51 pm

Re: Simple Calculations

Post by maverickalex » Tue Mar 01, 2011 11:19 am

I'm trying this script to convert a field which contains only seconds into hours:minutes and would like to then not display the seconds in the field "totaltime"

Code: Select all

--calculate time online
   
   put field "timeonline" into ttimeonline
      put the trunc of (ttimeonline) into tseconds
      put the trunc of (tseconds /60/60) into thours
      if thours < 1 then
      put the trunc of thours into tminutes
      end if
      if tminutes < 1 then
         put the trunc of thours & ":" & tminutes into field "totaltime"
      end if
I know its fairly close but not quite there. i'm displaying 0:0 in my totaltime field when my timeonline field shows 600. the correct value obviously would be 0:10

Can someone point me to the errors of my ways

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Simple Calculations

Post by bn » Tue Mar 01, 2011 2:46 pm

Hi maverickAlex,

this is one way of doing it:

Code: Select all

on mouseUp
   --calculate time online
   
   -- this gives you the format for display e.g. "00:36:05"
   set the numberformat to "00"
   put field "timeonline" into tTimeonline
   
   put tTimeOnline div (60*60) into tHours
   subtract tHours * (60*60) from tTimeOnline
   put tTimeOnline div 60 into tMinutes
   subtract tMinutes * 60 from tTimeOnline
   put tTimeOnline into tSeconds
   
   -- leave out the seconds if you dont want them
   put tHours & ":" & tMinutes & ":" & tSeconds into field "totaltime"
end mouseUp
kind regards

Bernd

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Simple Calculations

Post by Dixie » Tue Mar 01, 2011 3:00 pm

Hi...

This is another way :)

Code: Select all

on mouseUp
   -- assuming the seconds count is in fld 1
   put timeOnLine(fld 1)
end mouseUp

function timeOnLine timeCalc
   put trunc ((timeCalc/60)/60) into theHours
   put ((timeCalc/60) mod 60) into theMinutes
   
   set itemDel to "."
   
   put item 1 of theMinutes into theMinutes
   if theMinutes < 10 then put 0 & theMinutes into theMinutes
   put theHours & "." & theMinutes into totalTime
   
   return totalTime
end timeOnLine
be well

Dixie

maverickalex
Posts: 108
Joined: Sun Mar 15, 2009 11:51 pm

Re: Simple Calculations

Post by maverickalex » Wed Mar 02, 2011 8:00 am

Thanks to you both, they both work great,

Alex

Post Reply