a roundDown function
Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller
a roundDown function
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
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

 VIP Livecode Opensource Backer
 Posts: 5957
 Joined: Wed May 06, 2009 2:28 pm
 Location: New York, NY
Re: a roundDown function
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
I doubt whether this will get much traction in Scotland, but we will see what the community says...
Craig Newman

 Livecode Opensource Backer
 Posts: 3179
 Joined: Fri Feb 19, 2010 10:17 am
 Location: Bulgaria
Re: a roundDown function
Here's a thought:
No: I'm not being a sarcastic old soandso.
Ah:
"Just" chop off the offending decimal points?
No: I'm not being a sarcastic old soandso.
Ah:
"Just" chop off the offending decimal points?

 Livecode Opensource Backer
 Posts: 3179
 Joined: Fri Feb 19, 2010 10:17 am
 Location: Bulgaria
Re: a roundDown function
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

 VIP Livecode Opensource Backer
 Posts: 4944
 Joined: Sat Apr 08, 2006 8:31 pm
 Location: Minneapolis MN
 Contact:
Re: a roundDown function
There's a statRound() function that provides decimal precision.
Jacqueline Landman Gay  jacque at hyperactivesw dot com
HyperActive Software  http://www.hyperactivesw.com
HyperActive Software  http://www.hyperactivesw.com

 VIP Livecode Opensource Backer
 Posts: 1967
 Joined: Thu Feb 28, 2013 11:52 pm
 Location: Göttingen, DE
Re: a roundDown function
This is possibly what you want for rounding down:
To have *always* one decimal place (also for integers) you could use
This is good for currency (yields *always* two decimal places)
To have *always* one decimal place (also for integers) you could use
Code: Select all
put format("%.1f",var1) into fld 1
Code: Select all
put format("$%.2f",var1) into fld 1
shiftLock happens

 VIP Livecode Opensource Backer
 Posts: 2742
 Joined: Mon Jan 22, 2007 7:36 am
 Location: Berkeley, CA, US
 Contact:
Re: a roundDown function
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
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
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev