Forums >> Programming >> RPG Programming
Keeping a Leading Zero Before the Decimal Point with RPG and JSON
by: bvstone

Jump to: 






bvstone

Keeping a Leading Zero Before the Decimal Point with RPG and JSON

Posted:
Keeping a Leading Zero Before the Decimal Point with RPG and JSON

In doing a few JSON projects using YAJL I have run into the same issue a couple times.

JSON, it seems, requires a leading zero before the decimal point in a numeric value.  

For example:

"percent": 0.450

When using the %char Built in Function (BIF) the zero preceding the decimal point is removed.  So, the best option I have discovered is pulling an old trick out of the bag and using the %editw BIF.

Let's say we have a field defined a numeric with 5 digits and 3 decimal places:

D  tax_perc                      5  3 INZ   

If we try to use the following:

yajl_addChar('percent':%char(tax_perc)); 

We will end up with:

"percent": .450

But, if we use:

yajl_addChar('percent':%editw(tax_perc:'0 .   ')); 

Then we end up with the proper format required by JSON:

"percent": 0.450

Where you place the "0" in the edit word will tell the system when to stop zero suppressing the output.  In this case, it will stop right before the decimal.

I'm sure there are more ways to do this, but this is what has worked for me.  


Last edited 07/30/2017 at 15:39:58




Reply




Copyright 1983-2017 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, CKEditor and running on the IBM i (AKA AS/400, iSeries, System i).