Posts filed under 'Javascript'

Ceiling and Floor in Lingo

People occasionally have questions when trying to find a ceiling or floor function in the Lingo library so I figured I’d post it here for future googlers.

Lingo didn’t include these two math functions, but you can write your own functions and keep them in your personal library of goodies.

on floor aFloat
 return integer(aFloat) - (aFloat < integer(aFloat))
end

on ceil aFloat
 return integer(aFloat) + (aFloat > integer(aFloat))
end

If you are working with Javascript these functions are built in:

Math.ceil(aFloat);
Math.floor(aFloat);

2 comments February 3rd, 2009

Director, Math, and Javascript

In DirMX 2004 the ability to have scripts that make use of Javascript rather than Lingo was included. Although I’m from a Lingo background, I have found a few things that are much simpler to do in Javascript.

Lingoworkshop discusses Javascript in Director in terms of dealing with text and encryption. In my line of work one of my big concerns is display of numbers. Just like text, Javascript can help out with this.

In Lingo I’m sure you are used to seeing things such as this in the message window:
the floatPrecision = 4
put 3/4
-- 0
put 3./4
-- 0.7500

The first example is due to division of integers. In a math class 3/4 should not be equal to 0. In the latter example you are left with extra zeros due to the floatPrecision. In both cases I wouldn’t want my output text to show either of those values. I’d really like it to show 0.75.

Along comes Javascript. If you flip your message window over to Javascript you can type the following:
eval(3/4)
0.75
eval(3/5)
0.6
eval(3/7)
0.42857142857142855

This looks much better to the part of my brain that thinks in math. Using eval() makes getting simple expressions for display relatively easy. In Director there are ways to get these nicely formatted numbers, but nothing quite as quick as using the Javascript (from a coding point of view, I’ve never done any tests on the speed of this Javascript within Director).

One real world example where this can be used is in a basic calculator. In this particular case I did a search for “javascript calculator” and found many different implementations of Javascript for this purpose. I took the code and made a few modifications, added a few buttons, and one behavior (in Lingo) to deal with keys getting hit, and the final product can be used to do your taxes…or at least divide 3 by 4 with a nicely displayed number.

Click to view example (pop up window)

Download this example (zip file, 100k)

Add comment August 27th, 2006


Director Sites

Calendar

December 2024
M T W T F S S
« Feb    
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Archives

Recent Posts

Recent Comments