Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
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
stevedark
Partner Ambassador/MVP
Partner Ambassador/MVP

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

View solution in original post

5 Replies
hic
Former Employee
Former Employee

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
Author

Thanks to all.

Vlasta

Anonymous
Not applicable
Author

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

stevedark
Partner Ambassador/MVP
Partner Ambassador/MVP

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