Connection Information

To perform the requested action, WordPress needs to access your web server. Please enter your FTP credentials to proceed. If you do not remember your credentials, you should contact your web host.

Connection Type

The Round function with a double argument does not consistently round correctly – PB Docs 2022 – PowerBuilder Library

The Round function with a double argument does not consistently round correctly – PB Docs 2022

The Round function with a double argument does not consistently
round correctly

Symptom

Customer is using the PowerBuilder Round function with a Double
datatype argument, sometimes it rounds correctly and sometimes it does
not.

Environment

PowerBuilder

Reproducing the Issue

Using the following sample code, the result is not consistent for
the double values entered.

tr_027.jpeg

Cause

This is not a PowerBuilder bug, it is expected behavior. The PB
Round function is returning a decimal. To assure that you are getting
the correct rounded value, use an argument with a decimal
datatype.

Double and Decimal datatypes have different internal formats to
store numbers. A PB Double datatype is an approximate numeric datatype
and is subject to rounding errors after arithmetic operations. A PB
Decimal data type is an exact numeric datatype. Only exact numeric data
types are guaranteed to be accurate to the least significant digit
specified after arithmetic operations. A Decimal datatype is a fixed
point number. It is designed for financial calculation and should be
used when accuracy is required such as in financial fields. In this
specific case, where you are using the PB Round function, you should
always use a decimal datatype argument to assure an accurate rounded
value returned from the function.

This same behavior can be seen in other programming languages. As
an example, this same behavior can be seen when passing a Double to the
Math.Round(Double, Int32) method in a C# or VB application.

Solution

You should always pass a decimal datatype to the PB Round()
function. If you need to round a double value, you can use the code
below to cast it first to a string and then to a decimal.

tr_028.jpeg


Document get from Powerbuilder help
Thank you for watching.
Was this article helpful?
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x