Page 1 of 1

Rounding Decimal values

Posted: Thu Jul 12, 2007 3:12 pm
by DS4DRIVER
I use a Routine to Round Decimal values. The code is as follows

Value and Digits are the paameters passed to the routine

Code: Select all

If Digits > 0 then
 Ans = FIX(Value, Digits)
End Else
 Ans = FIX(Value/100,50)*100
End
I also tried

Code: Select all

PRECISION 14
If Digits > 0 then
 Ans = FIX(Value, Digits)
End Else
 Ans = FIX(Value/100,50)*100
End
When i try the following example it does not work like how i want it
Example 1:
Value = 123456789.789947874545
Digit = 10
Result = 123456789.789948
The routine rounds of at the 6th position.
I was expecting result to be 123456789.7899478745 (round at the 10th position)

Example 2:
Value = 123456.789947874545
Digit = 10
Result = 123456.789947875
The routine rounds of at the 9th position.
I was expecting result to be 123456.7899478745 (round at the 10th position)

Example 3:
Value = 12345.789947874545
Digit = 10
Result = 12345.7899478745
The routine rounds of at the 10th position.
The result is as expected 123456789.7899478745 (round at the 10th position)

Looks like it it something to do with the Integer part of the Decimal value, when the value was increased from 12345.789947874545 to 123456.7899478745, there was a difference in rounding.

Is there any other optional way to make the Round work the way i want.

Posted: Thu Jul 12, 2007 3:46 pm
by ray.wurlod
Fmt(expression, "20R9") will give you nine digits after the decimal place. You may wish to apply a TrimF() function to that result.

Re: Rounding Decimal values

Posted: Mon Jul 16, 2007 2:49 pm
by girish119d
Fmt(expression, "R10")

Try above thig as well
DS4DRIVER wrote:I use a Routine to Round Decimal values. The code is as follows

Value and Digits are the paameters passed to the routine

Code: Select all

If Digits > 0 then
 Ans = FIX(Value, Digits)
End Else
 Ans = FIX(Value/100,50)*100
End
I also tried

Code: Select all

PRECISION 14
If Digits > 0 then
 Ans = FIX(Value, Digits)
End Else
 Ans = FIX(Value/100,50)*100
End
When i try the following example it does not work like how i want it
Example 1:
Value = 123456789.789947874545
Digit = 10
Result = 123456789.789948
The routine rounds of at the 6th position.
I was expecting result to be 123456789.7899478745 (round at the 10th position)

Example 2:
Value = 123456.789947874545
Digit = 10
Result = 123456.789947875
The routine rounds of at the 9th position.
I was expecting result to be 123456.7899478745 (round at the 10th position)

Example 3:
Value = 12345.789947874545
Digit = 10
Result = 12345.7899478745
The routine rounds of at the 10th position.
The result is as expected 123456789.7899478745 (round at the 10th position)

Looks like it it something to do with the Integer part of the Decimal value, when the value was increased from 12345.789947874545 to 123456.7899478745, there was a difference in rounding.

Is there any other optional way to make the Round work the way i want.