So - I had some time to read, research and try out some things.
The problem results from two rules of RPG expressions:
1st rule
The ** operator (power) always results in a double precision float
(intermediate) result.
-> [1]
https://www.ibm.com/docs/en/i/7.5.0?topic=fnir-operator
2nd rule
The (intermediate) result of the * operator (multiplication) is a float
value if one of the operands is a float value.
-> [2]
https://www.ibm.com/docs/en/i/7.5.0?topic=results-operators
And the real problem is your value 1234567.89 - because it's not
convertible to or from float without an rounding error - or approximation
- or nearest representation.
You can try your values with a conversion tool
like [3]
https://www.binaryconvert.com.
For the value [4]1234567.89 the nearest double float representation
(approximation) is 1.2345678899999998975545167923 E+006.
And because the intermediate result is a double float (according to 1st
and 2nd rule) you get that error.
So as a simple rule of thumb - whenever possible, stay away from (double)
float values or (intermediate) results.
I hope this clarified the problem - it's not the 10^2 (10**2) - it's the
1234567.89 value.
HTH and kind regards,
Daniel
Am 18.06.2025 um 21:11 schrieb Raul Alberto Jager Weiler
<raul.jager@xxxxxxxxx>:
Also you can use the bif %editc to convert to character, without
comas
nor points.
I think the code must be z, not trust my memory.
El mar, 17 de jun de 2025, 19:17, Jon Paris <jon.paris@xxxxxxxxxxxxxx>
escribió:
How many different scenarios are you attempting to accommodate?
The way you have it coded you need to change the code for each and
every
scenario so I'd be inclined to take a simpler approach that avoids
multiplication altogether.
Something like:
Ctl-Opt DftActgrp( *No );
Dcl-Ds convertIt;
number zoned(15:5);
char char(15) SamePos(number);
End-Ds;
dcl-s myNumber1 packed(7:2) inz(12345.67);
dcl-s myNumber2 packed(9:4) inz(12345.6789);
number = myNumber1;
Dsply ( '#1 is: *' + char + '*');
number = myNumber2;
Dsply ( '#2 is: *' + char + '*');
*InLr = *On;
This is not _the_ answer but it might get you thinking. If you
enclosed
this in a subprocedure and fed it the decpos etc. as a parm it would
be
quite simple to make it generic.
It won't work for negative numbers but ...
Jon Paris
Jon.Paris@xxxxxxxxxxxxxx
On Jun 17, 2025, at 2:55 PM, David Gibbs via RPG400-L <
rpg400-l@xxxxxxxxxxxxxxxxxx> wrote:
Folks:
I'm in the process of creating a routine that needs to adjust
different sized numeric fields to be whole integers.
To do this, I'm multiplying the value times 10 raised to the number
of
decimal positions.
So, if I have 12345.67, I multiply by 100 and get 1234567.
To make this easily adaptable, I use the %DECPOS bif to get the
number
of decimal positions.
For example:
dcl-s digits2 zoned(20:2) inz(1234567.89);
result = digits2 * (10 ** %decpos(digits2));
The problem is, if the number of decimal positions is 2, the
calculation isn't working correctly.
In the above case, the result is 123456788.
HOWEVER, if I use %int on the result of the multiplier, I do get the
correct result.
result = digits2 * %int(10 ** %decpos(digits2));
This results in 123456789.
As best as I can determine, the result of the '10 **
%decpos(digits2)'
is a float and is causing a rounding error of some sort.
Here's a sample program that you can step through in the debugger.
https://code.midrange.com/ad462aef4a.html
Any thoughts?
Thanks!
david
--
IBM i on Power Systems: For when you can't afford to be out of
business!
I'm riding in the American Diabetes Association's Tour de Cure to
raise money for diabetes research, education, advocacy, and
awareness.
You can make a tax-deductible donation to my ride by visiting
https://mideml.diabetessucks.net.
References
Visible links
1.
https://www.ibm.com/docs/en/i/7.5.0?topic=fnir-operator
2.
https://www.ibm.com/docs/en/i/7.5.0?topic=results-operators
3.
https://www.binaryconvert.com/result_double.html?decimal=049050051052053054055046056057
4.
https://www.binaryconvert.com/result_double.html?decimal=049050051052053054055046056057
As an Amazon Associate we earn from qualifying purchases.