Page 1 of 1
Formatting Decimal to String conversion
Posted: Thu May 05, 2005 9:32 am
by blewip
I am performing a type conversion from a Float to a String using the following
Code: Select all
Trim(DecimalToString(DSLink2.NOMIN_QTY,"fix_zero,suppress_zero"))
This works fine apart from the fact that I have no control over the number of decimal places.
For example the output may look like 14256.43676
When I would want it to look like 14256.44
Is there a way to do this?
Posted: Thu May 05, 2005 11:23 am
by amsh76
Why don't you round it to two decimal places first, and then do the conversion from float to string.
Posted: Fri May 06, 2005 2:35 am
by blewip
Thanks, yes that does kinda work. The Float is already defined as 15,2, however the scale may be meaningless for a Float. However if I convert the Float to a Decimal 15,2 and then do the DecimalToString in another Transformer this does indeed work (an extra transformer though).
I can also do the following
Code: Select all
Trim(DecimalToString((Fabs ((DSLink2.NOMIN_QTY*100))/100),"fix_zero,suppress_zero"))
This rounds to two decimal places first as well and this works fine (in same transformer). It just seems a bit long winded.
I was hoping they'd be an easier way. The online manual gives the following arguments for the DecimalToString and I was hoping one of these might help, however I can't get any of them to work (except fix_zero). Also suprisingly the suppress_zero is not documented, is it documented anywhere else, how did anyone find this argument?
Code: Select all
number (decimal)
[ fixzero(int8)]
[prefix (string)]
[prefixlen (int32)]
[suffix (string)]
[suffixlen (int32)](
Posted: Fri May 20, 2005 2:16 am
by blewip
I had a few problems with the old solution, and I found a better way. There appears to be a new function DFloatToStringNoExp, this converts to string. doesn't give you an exponential and allow you you have control over the number of decimal places.
Code: Select all
DFloatToStringNoExp(DSLink2.FLOAT2DECI,6)
I sure people are using it, but it just doesn't appear in the documentation.
I also wanted to recipricol of a Float and found that it was better to convert to a String and then do the Maths.
Code: Select all
1/DFloatToStringNoExp(DSLink2.FLOAT2DECI,6)
Seems slightly odd, but 1/100 was giving me 0.0099999999 before.