a roundDown function

Something you want to see in a LiveCode product? Want a new forum set up for a specific topic? Talk about it here.

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller, kevinmiller

Post Reply
jmk_phd
Posts: 27
Joined: Sat Apr 15, 2017 8:29 pm

a roundDown function

Post by jmk_phd » Sun Feb 18, 2018 3:32 am

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5314
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: a roundDown function

Post by dunbarx » Sun Feb 18, 2018 7:59 am

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

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 2432
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

Re: a roundDown function

Post by richmond62 » Sun Feb 18, 2018 9:13 am

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?

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 2432
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria
Contact:

Re: a roundDown function

Post by richmond62 » Sun Feb 18, 2018 9:34 am

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 17 times

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4376
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: a roundDown function

Post by jacque » Sun Feb 18, 2018 5:49 pm

There's a statRound() function that provides decimal precision.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1682
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: a roundDown function

Post by [-hh] » Wed Feb 21, 2018 10:33 pm

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
shiftLock happens

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2673
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: a roundDown function

Post by mwieder » Thu Feb 22, 2018 1:58 am

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

Post Reply

Return to “Feature Requests”