2

I am looking for a way to convert a floating-point value to scientific notaiton string in Beckhoff TwinCAT or codesys. The documentation for their FB_FormatString says this is not currently supported. Does anyone have a "goto" approach for this?

Mark Lazarides
  • 366
  • 2
  • 13
  • That is too bad it is not supported yet. I guess I would turn the number into a string with `str := TO_STRING(the_lreal)`. Then make a new string by 1. finding the index of the . in `str`, 2. then make the new string where you take `str[0] + "."` + the rest of `str[1:]` minus the index where the string is. And then add the case where the first number is a 0, so you would need to search the string for the first occurrence of a non-zero. – Roald Oct 13 '22 at 11:36
  • 1
    Have you tried something like this: `mystring = LREAL_TO_STRING(myRealNumber);` ? The [documentation](https://infosys.beckhoff.com/english.php?content=../content/1033/tcplccontrol/925585291.html&id=6584232285818866881) points out that it's possible, but it doesn't give an example for scientific notation, In [Codesys](https://help.codesys.com/api-content/2/codesys/3.5.17.0/en/_cds_operator_real_to/#a34c7ecbeec3bac0a8640e014adbad-id-0c67c09c2a481671c0a8646363b155fe) there is something like this and it works for scientific notation, I just don't know if it would be the same in Twincat. – dwpessoa Oct 13 '22 at 18:04

1 Answers1

2

In TwinCAT I use LREAL_TO_FMTSTR

sOut := LREAL_TO_FMTSTR( 0.46523, 2, TRUE );
user16217248
  • 3,119
  • 19
  • 19
  • 37