Page 1 of 1

a roundDown function

Posted: Sun Feb 18, 2018 3:32 am
by jmk_phd
It would be nice to have a roundDown function in LC, similar to the the ROUNDDOWN in Excel and other spreadsheets.

For now I was able to work around this by using the LC trunc() function, inasmuch as the result needed was a whole number (i.e., sans decimals). But I can at least imagine a case in which rounding down to a single decimal place might be needed -- doable currently in LC, but requiring some acrobatics.

Thanks.

jeff k

Re: a roundDown function

Posted: Sun Feb 18, 2018 7:59 am
by dunbarx
As you say, existing native LC gadgetry can do this pretty easily. You do know how to use the "round" function with both positive and negative parameters, right?

I doubt whether this will get much traction in Scotland, but we will see what the community says...

Craig Newman

Re: a roundDown function

Posted: Sun Feb 18, 2018 9:13 am
by richmond62
Here's a thought:
round.png
No: I'm not being a sarcastic old so-and-so.

Ah:
RDExcel.png
"Just" chop off the offending decimal points?

Re: a roundDown function

Posted: Sun Feb 18, 2018 9:34 am
by richmond62

Code: Select all

on mouseUp
   ask "How many decimal points"
   put it into DPOINTS
   put fld "fNUM" into CIPHER
   put (CIPHER mod 1) into CEND
   put (CIPHER - CEND) into CTOP
   put CTOP into fld "fOUT"
   put "." after fld "fOUT"
   delete char 1 of CEND
   delete char 1 of CEND
   repeat with XX=1 to DPOINTS
      if char XX of CEND is not empty then
         put char XX of CEND after fld "fOUT"
         end if
   end repeat
end mouseUp
RD.png
Round Downer.livecode.zip
(1.27 KiB) Downloaded 188 times

Re: a roundDown function

Posted: Sun Feb 18, 2018 5:49 pm
by jacque
There's a statRound() function that provides decimal precision.

Re: a roundDown function

Posted: Wed Feb 21, 2018 10:33 pm
by [-hh]
This is possibly what you want for rounding down:

To have *always* one decimal place (also for integers) you could use

Code: Select all

put format("%.1f",var1) into fld 1
This is good for currency (yields *always* two decimal places)

Code: Select all

put format("$%.2f",var1) into fld 1

Re: a roundDown function

Posted: Thu Feb 22, 2018 1:58 am
by mwieder
Depends on your needs. Rounding and truncating are different operations.

The difference is that the statRound() function will return the *nearest* value with reference to the desired number of decimal places, and for negative values the *nearest* may not be the *next lower*.

The trunc() function will return just the integer part of the input value, which will be the next *lower* integer for positive values and the next *higher* integer for negative values.

The floor() function will correctly provide the next *lower* integer value for both positive and negative input values, but this is different from a rounding function in that it may not provide the *nearest* integer.

round(3.2) => 3
round(3.7) => 4
statRound(3.149265,2) => 3.14
statRound(3.987654,2) => 3.99
trunc(3.2) => 3
trunc(3.7) => 3
floor(3.2) => 3
floor(3.7) => 3

round(-3.2) => -3
round(-3.7) => -4
statRound(-3.149265,2) => -3.14
statRound(-3.987654,2) => -3.99
trunc(-3.2) => -3
trunc(-3.7) => -3
floor(-3.2) => -4
floor(-3.7) => -4