Cancel
Showing results for 
Search instead for 
Did you mean: 

Betreff: How do you perform formatted string conversion?

Genius
Genius

OK - not done yet. Unexpected behaviour with
num_to_str( <real>, <precision> )

Look here :

// #009//;
// #.01//;
// #011//;


I don't think that "precision" and "significant digits" are synonymous in the function description below, but be that as it may,

num_to_str(real [,integer]) --> string
converts the number designated by the parameter of data type real into a string
...the optional parameter designates the precision, i.e., the number of significant digits, which the resulting string has at the most.


So next post has the revised solution.
Am crediting myself with the Accepted Solution (if the forum rules allow that).

 

____________________________________________
PlantSim Ver 14.1.0.1294   &  Windows 7 Ent. SP1

~~~~  1st post in thread  ~~~~

07=How do you perform formatted string conversion?

Genius
Genius

// File: ,L,U,SprintfInt.txt
// Use:  .L.U.SprintfInt( intVal, numDigits) returns <string>
//                   with intVal rendered using numDigits digits  

param intVal : integer,  numDigits : integer   ->   string

/*   Examples :  

.L.U.SprintfInt(  7, 3)  -->  007
.L.U.SprintfInt( 10, 3)  -->  010

Sample usage :

fTxt.writeRow( 1, iRowWt, "// #" +.L.U.SprintfInt( iTem, 3) +"//;"
+to_str(obj) +".Label := "      + to_str(  obj.Label)+"; " )

   Inconsistency in num_to_str( , ) implementation, so force an extra significant digit and trim this out
   num_to_str(real [,integer]) --> string
   converts the number designated by the parameter of data type real into a string
   ...the optional parameter designates the precision, i.e., the number of significant digits, which the resulting string has at the most.
*/

var str := num_to_str( (intVal +0.4) / pow( 10, numDigits), numDigits)

// Modified form of Ralf Tobel solution
return copy( str, strlen(str) -numDigits, numDigits)

// AlexD solution - now requires two omit(,) calls - prefer the above by Occam's principle
// return omit( str, 1, strlen( str) -numDigits)

Betreff: How do you perform formatted string conversion?

Genius
Genius

< revised solution>

 

// File: ,L,U,SprintfInt.txt
// Use:  .L.U.SprintfInt( intVal, numDigits) returns <string>
//                   with intVal rendered using numDigits digits  

param intVal : integer,  numDigits : integer   ->   string

/*   Examples :  

.L.U.SprintfInt(  7, 3)  -->  007
.L.U.SprintfInt( 10, 3)  -->  010

Sample usage :

fTxt.writeRow( 1, iRowWt, "// #" +.L.U.SprintfInt( iTem, 3) +"//;"
+to_str(obj) +".Label := "      + to_str(  obj.Label)+"; " )

   Inconsistency in num_to_str( , ) implementation, so force an extra significant digit and trim this out
   num_to_str(real [,integer]) --> string
   converts the number designated by the parameter of data type real into a string
   ...the optional parameter designates the precision, i.e., the number of significant digits, which the resulting string has at the most.
*/

var str := num_to_str( (intVal +0.4) / pow( 10, numDigits), numDigits)

// Modified form of Ralf Tobel solution
return copy( str, strlen(str) -numDigits, numDigits)

// AlexD solution - now requires two omit(,) calls - prefer the above by Occam's principle
// return omit( str, 1, strlen( str) -numDigits)

Betreff: How do you perform formatted string conversion?

Gears Phenom Gears Phenom
Gears Phenom
Why don‘t you use the StrRcopy method? Would be more straightforward than the copy/omit approaches.

Cheers
Alex
___________________________________________________________
Alex Dilg, Consultant at SimPlan AG (www.SimPlan.de)
This post was good and/or helpful to you? Please provide some Kudos, thank you!

Betreff: How do you perform formatted string conversion?

Siemens Phenom Siemens Phenom
Siemens Phenom

You're absolutely right, strrcopy does exactly what he wants.

Regards,

Ralf
------------------------------------------------------------------------------------------------------
Did you like the answer? Then click the Thumbs Up button.
Did the answer solve your problem? Then accept the answer as solution.

Betreff: How do you perform formatted string conversion?

Genius
Genius
Thanks for the lead - will follow that up

Betreff: How do you perform formatted string conversion?

Genius
Genius
> Syntax: strRcopy(SourceText:string, NumberOfCharacters:integer) → string

aka precisely what I asked earlier if there were a right( , ) function.

However this is largely nugatory now, as I need to trim the extra digit added to cater for the behaviour of num_to_str regarding following significant digits and not precision.

Old_Sohol Pioneer

@ 2018-05-07(Mon) Z14:48:54.927

Thanks Ralf,

So if there were a right( <str : string>, <digits : integer>) function, then I could implement
to_str( varName, "%3.3d")

as right( num_to_str( varName /1e3, 3), 3)