SuperCard 4.8 Enhanced Rounding

Need help with a script? This is the place to discuss how to get your code running!

SuperCard 4.8 Enhanced Rounding

Postby codegreen » Mon Mar 28, 2016 9:23 am

SuperCard 4.8b11 introduces two new optional parameters that powerfully extend SuperTalk's round function.

The first specifies the number of decimal places (which is implicitly zero).

Code: Select all
get round(<value>, <places>)

If you pass a negative number, <value> will be rounded abs(<places>) to the left of the decimal point.

For example:

Code: Select all
put round(123.456)       -- 123
put round(123.456, 2)    -- 123.46
put round(123.456, -1)   -- 120

The second specifies the rounding mode.

Code: Select all
get round(<value>, <roundingMode>)

There are four modes:

plain (the default) is the one they taught you in second grade: any fraction ≥ 0.5 gets rounded up

down means always round downward

up means always round upward

even means round toward even (often called banker's rounding)

Rounding to even means that instead of always rounding 5 up or down you round it to the nearest even number, which (at least for randomly odd/even input) prevents upward bias from creeping into calculations.

To see it in action, try this:

Code: Select all
on mouseUp
  local theFloats, thePlains, theEvens
  repeat with i = 1 to 10
    get i + 0.5
    put it & cr after theFloats
    put round(it) & cr after thePlains
    put round(it, even) & cr after theEvens
  end repeat
  ask list catcols(theFloats & cr & sum(replace(trim(theFloats), cr, comma)), ¬
    thePlains & cr & sum(replace(trim(thePlains), cr, comma)), ¬
    theEvens  & cr & sum(replace(trim(theEvens), cr, comma)))
end mouseUp

function catcols
  enum numCols = paramCount(), numLines = 0
  local outstr
  repeat with i = 1 to numCols
    put max(numLines, the num of lines of param(i)) into numLines
  end repeat
  repeat with i = 1 to numLines
    repeat with j = 1 to paramCount()
      put line i of param(j) after outstr
      put tab after outstr
    end repeat
    put cr into last char of outstr
  end repeat
  return outstr
end catcol

But if there are two optional parameters, in what order should you pass them if you want to use both? And what mnemonic device will you need to embed this obscure detail like a tick in your poor overworked memory (most likely at the expense of something useful like your online banking password)?

Well just pass them in whatever order you like of course! This is SuperTalk… ;-)

Posts: 1534
Joined: Mon Jul 14, 2008 11:03 pm

Return to Scripting in SuperTalk

Who is online

Users browsing this forum: No registered users and 1 guest