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.

Moderator: Klaus

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

a roundDown function

Post 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
dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10510
Joined: Wed May 06, 2009 2:28 pm

Re: a roundDown function

Post 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
richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10433
Joined: Fri Feb 19, 2010 10:17 am

Re: a roundDown function

Post 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?
richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10433
Joined: Fri Feb 19, 2010 10:17 am

Re: a roundDown function

Post 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 509 times
jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7423
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: a roundDown function

Post by jacque »

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: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: a roundDown function

Post 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
shiftLock happens
mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3582
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: a roundDown function

Post 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
Post Reply