|
Tim wrote: >This may have been answered beofre, but I just wanted to verify what I did >is ok, or if I should have done it a different way. > >I am reading a comma delimited file, and one of the fields is a dollar >amount. I want to take the dollar amount which is now in a text string of >10 character or so, ie: '123.45' > >and I want to convert it to a zoned 17,2 but when I used the atof operation, >I was always get a value 1 penny less that the string that I initially read. > >So I added the (H) to the eval command and it solved the problem.. >c eval(h) CheckAmount = atof(fld8) > >My question is this, is that OK? Or should I get the dollar amount from the >string a different way? >Also I did add the H spec to respect decimal position, but that didn't help >the problem... This is working pretty much as expected. Floating point is generally used for modelling physical processes involving measured values that are typically accurate to only so many significant digits. As a result, floating point values are inherantly inaccurate. Most integral values, such as monetary amounts cannot be represented with 100% accurate precision. Regarding using half-adjust, that should be fine provided the precision of your result is not too great. Before putting your code into production, make sure you feed it the maximum possible numeric value into the character field and see what happens. I think you'll find that the maximum number of digits you can safely work with is about 15. Any more than that and you risk either losing precision in the least significant digits or numeric overflow. Regarding the H-Spec option EXPROPTS(*RESDECPOS), that only has an effect for decimal expressions. Since you're dealing with a float value, it has no effect here. I'd recommend using a different technique to convert char strings into decimal values. Although atof and half-adjust may work with reasonably small values, it doesn't scale higher than around 15 digits. Now you may well be aware of that limitation, but the next programmer who works on your code may not be. At the very least, document the limits clearly when using the atof technique. Cheers! Hans Hans Boldt, ILE RPG Development, IBM Toronto Lab, boldt@ca.ibm.com +--- | This is the RPG/400 Mailing List! | To submit a new message, send your mail to RPG400-L@midrange.com. | To subscribe to this list send email to RPG400-L-SUB@midrange.com. | To unsubscribe from this list send email to RPG400-L-UNSUB@midrange.com. | Questions should be directed to the list owner/operator: david@midrange.com +---
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.