decimal variable force 2 decimal places

Posted by rayherring on 12-Jun-2016 23:05

Is there any way to force a decimal variable to 2 decimal places?

I'm trying to generate a hash of all necessary fields in a temp-table for a web-service request that I have to make, if I check the temp-table by doing 'write-xml' it shows all decimal fields as 2 decimal places, however when I pull those values out and stick them into a character delimited by | for each field it drops zeroes and decimal places off completely.

19.00 becomes 19
19.10 becomes 19.1

etc...

Only way I have worked out so far is to convert the decimal to a string using the format ">,>>>,>>9.99" and then left-trim all the extra blank spaces off.

Posted by Simon L. Prinsloo on 13-Jun-2016 02:43

Hi Ray,

When you STRING a decimal, it normally converts  to the minimum number of digits needed to display it.

Your solution sounds like the correct one for what you need here.

To be clear, you should use TRIM(STRING(myDecimal,"->>>,>>>,>>9.99")).

Take care to rather supply more than less digits than you need as well as the sign, otherwise large numbers or negative values will cause run time errors. and if the XML does not have group separators you would most likely want to remove that from the format as well.

All Replies

Posted by Simon L. Prinsloo on 13-Jun-2016 02:43

Hi Ray,

When you STRING a decimal, it normally converts  to the minimum number of digits needed to display it.

Your solution sounds like the correct one for what you need here.

To be clear, you should use TRIM(STRING(myDecimal,"->>>,>>>,>>9.99")).

Take care to rather supply more than less digits than you need as well as the sign, otherwise large numbers or negative values will cause run time errors. and if the XML does not have group separators you would most likely want to remove that from the format as well.

Posted by rayherring on 13-Jun-2016 03:10

Thanks for that, was hoping there was an easier way (like in Javascript where I can do ".toFixed()') but that is ok, will also change from 'LEFT-TRIM' to just 'TRIM'.

Posted by Robin Brown on 13-Jun-2016 06:42

How about using the DECIMALS option?

DEF VAR myDecimal AS DECIMAL DECIMALS 2.

Posted by rayherring on 13-Jun-2016 07:09

Already tried, unfortunately I have to build a string delimited by | of certain fields, which means converting them to strings, and the decimal gets lost for any values that didn't have much after it.

decimal decimals 2 works fine for the temp-table and the resultant xml from it. What's even more annoying though is when I use 'display myDecimalVar' it always shows 2 decimal places, but when you 'message myDecimalVar' to either the screen using 'mpro' or to a log file, all zeroes after the decimal point are removed as is the decimal point if there is nothing left after it.

Posted by Tim Kuehn on 13-Jun-2016 07:21

You can get the TT field format like so:

  buffer tt1:handle:buffer-field("f1"):format

Save that in a local var and then use as required.

This thread is closed