Round decimal don't work

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
JosepM
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 344
Joined: Tue Jul 20, 2010 12:40 pm

Round decimal don't work

Post by JosepM » Tue Oct 18, 2016 9:32 am

Hi,

I have a big problem with the round function.

I'm with a old version, LiveCode 4.6.4

Theoretically the round function must make a financial rounding, ie, if the decimal part ends in 5, should increase one up.

round (398,545.2) -> 398.55 right?

Any solution or how patch it?

Salut,
Josep M

Klaus
Posts: 11825
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Round decimal don't work

Post by Klaus » Tue Oct 18, 2016 9:47 am

Hi Josep,

you are mixing COMMA and DOT here! :D

THIS: put round (398,545.2) -> gives an error in LC 8.x
BUT: put round (398.545,2) -> 398,55

LC is "speaking english" so the DOT is used as the decimal delimiter and
the comma to separate the PARAMETER from the data in this function.


Best

Klaus

JosepM
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 344
Joined: Tue Jul 20, 2010 12:40 pm

Re: Round decimal don't work

Post by JosepM » Tue Oct 18, 2016 9:55 am

Hi,
I know, it's a typo posting on the forum :( sorry
With 4.6.4 don't work, with 6.6.2 work correct.

On 4.6.4 the result is 398.54, don't up

Any solution?

Klaus
Posts: 11825
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Round decimal don't work

Post by Klaus » Tue Oct 18, 2016 1:53 pm

Hi Josep,

well, that would have been too easy, right? :D
Sorry, no idea how to workaround this, if possible at all.


Best

Klaus

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

Re: Round decimal don't work

Post by dunbarx » Tue Oct 18, 2016 1:58 pm

Hi.

Perhaps you are referring to the HyperTalk way of rounding, which was NOT the financial way, where the integer part determined upward or downward rounding when the decimal part was halfway between?

But LC has another function "statRound", and you can use this if you wish.

Hypertalk recommended this to always round up:

Code: Select all

function roundUp var
if var < 0 then return trunc(var - 0.5) else return trunc(var + 0.5)
end roundup
Craig Newman

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

Re: Round decimal don't work

Post by [-hh] » Tue Oct 18, 2016 8:33 pm

Below the version of Craig, for Cents. He's thinking in integers with money :-)
Works in every version of LC (more correctly: in every version I know).

Code: Select all

-- input:  any number
-- output: rounded and formatted to two decimals
-- rounds positive up, negative down, 
function financialRound myNum
  if myNum < 0 then
    put trunc(100*myNum-0.5) into rslt
  else
    put trunc(100*myNum+0.5) into rslt
  end if
  put "." after char -3 of rslt
  return rslt
end financialRound
shiftLock happens

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”