Zahlen Runden


[ Zettels Traum ] [ search / suche ]

von dp am 22.Dezember 96 um 19:23:06:

zu: Perlen aus dem Strom der Nachrichten im direct_L von Daniel am 22.Dezember 96 um 02:00:59:

>I'm working out a formula for calculating costs, and I want to round off
>the resulting number to 2 decimal points. Any ideas??

Here's a general purpose handler for rounding off to any number of
decimal points.

on limitDigits theNumber, thePlaces, floatOrString
--Input validation, to prevent bad usage and script error alerts
if not (floatP(theNumber) or integerP(theNumber)) then
return "Error: theNumber not a number"
end if
if not integerP(thePlaces) then
return "Error: thePlaces not an integer"
end if
if thePlaces < 1 then
return "Error: thePlaces must be 1 or greater"
end if
if not ((floatOrString = #float) or (floatOrString = #string)) then
return "Error: floatOrString must be #float or #string"
end if
set oldDelim = the itemDelimiter
set oldPrecision = the floatPrecision
set the itemDelimiter = "."
set the floatPrecision = thePlaces
set theString = string(float(theNumber))
set left = item 1 of theString
set right = item 2 of (theString & "000000000000000000000000000")
--The extra 0's assure full width when the decimal portion is 0
set theAnswer = left & "." & char 1 to thePlaces of right
if floatOrString = #float then set theAnswer = float(theAnswer)
set the itemDelimiter = oldDelim
set the floatPrecision = oldPrecision
return theAnswer
end

It can be used like this...

put limitDigits(1.0515,3, #string)
-- "1.052"
put limitDigits(1.0515,3, #float)
-- 1.0520

Note that if you ask for a floating point answer, it will appear that
the function didn't exactly work. But it's important to understand
the use of 'the floatPrecision'.

Float values are stored and calculated without regard to the
setting of 'the floatPrecision'. Only the conversion of float
values into strings is affected by 'the floatPrecision'. So
the handler above is returning a floating point value, and
the message window itself is showing four places when it
writes the string of the value into the window. Note that the rounding has
been carried out as appropriate. By asking for a float value
answer, you can carry out further additions and multiplications
on the result that will truly reflect the results of the rounding.

This subtlety of 'the floatPrecision' is also what's behind Steve
Taylor's comment:

>set x = 123.456789
>set x = (integer(x * 100))/100.0
>put x
>-- 123.4600
>
>Hmmm... that gets it down to 2 decimal points of data, but put seems to
>like those extra zeroes...


Note that the previous setting of 'the itemDelimiter' and
'the floatPrecision' are stored and restored in the handler
above, to prevent this handler from having unintended side
effects in other parts of your movie.

You could remove all the idiot-proofing parameter checking once
you're sure your Lingo scripting is correct.

Also note that you should never set 'the floatPrecision' to 0, or
you will get strange results:

set the floatPrecision = 4
put float("1.0515")
-- 1.0515
set the floatPrecision = 0
put float("1.0515")
-- 1.05150000000000010130


- Glenn M. Picher



Dazu:























D. Plänitz