Qlik Community

QlikView Scripting

Discussion Board for collaboration on QlikView Scripting.

Not applicable

Round() function giving sometimes incorrect results

Hello

Please look into attached .qvw file.

There is a text object within, rounding numbers to two decimal places, some of them correctly, some of them incorrectly.

Can anybody explain it?

Is it known bug?

QV 11.20.12018.0 SR3 64-bit

Many thanks in advance.

Vlastimil

1 Solution

Accepted Solutions
MVP
MVP

Re: Round() function giving sometimes incorrect results

Taking Bill Markham 's idea further.  You could add a much smaller number, to ensure you don't get a different number to what you expect.

You can also put this into a variable.  So, if you create a variable called vMyRound with the following in:

round($1 + 0.0000000001, $2)

You can then call it in an expression like this:

$(vMyRound(14.235, 0.01))

The technique of passing parameters into a variable is incredibly useful, thanks to Stephen Redmond for bringing it to my attention some time back.

Steve

5 Replies
MVP
MVP

Re: Round() function giving sometimes incorrect results

Employee
Employee

Re: Round() function giving sometimes incorrect results

The short answer is: No it is not an error. This is expected behaviour of software that uses a binary number representation internally. What happens is that Round() rounds to nearest binary number - not to nearest decimal number.

Read the blog post behind Massimo's link, and you will understand.

HIC

Not applicable

Re: Round() function giving sometimes incorrect results

Thanks to all.

Vlasta

Re: Round() function giving sometimes incorrect results

Hi

This expression works, as in adding 0.001 to number and rounding to 0.01.

     round( (1.235+0.0001 ), 0.01 )

It is horrible frig, maybe somebody will have a more elegant solution.

Best Regards,     Bill

MVP
MVP

Re: Round() function giving sometimes incorrect results

Taking Bill Markham 's idea further.  You could add a much smaller number, to ensure you don't get a different number to what you expect.

You can also put this into a variable.  So, if you create a variable called vMyRound with the following in:

round($1 + 0.0000000001, $2)

You can then call it in an expression like this:

$(vMyRound(14.235, 0.01))

The technique of passing parameters into a variable is incredibly useful, thanks to Stephen Redmond for bringing it to my attention some time back.

Steve

Community Browser