a roundDown function
Moderator: Klaus
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
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: 10080
- Joined: Fri Feb 19, 2010 10:17 am
Re: a roundDown function
Here's a thought:
No: I'm not being a sarcastic old so-and-so.
Ah:
"Just" chop off the offending decimal points?
No: I'm not being a sarcastic old so-and-so.
Ah:
"Just" chop off the offending decimal points?
-
- Livecode Opensource Backer
- Posts: 10080
- Joined: Fri Feb 19, 2010 10:17 am
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
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
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
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