Cancel
Showing results for 
Search instead for 
Did you mean: 

007=How do you perform formatted string conversion?

Genius
Genius

Hi,

I'd like to do the equivalent of this please

to_str( varName, "%3.3d")

so that 7 is output as 007.

Sorry about the first posting of this in the "Articles" page (for those who saw it), been busy on something else for a while, a bit rusty here  Smiley Surprised

____________________________________________
PlantSim Ver 14.1.0.1294   &  Windows 7 Ent. SP1

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

16 REPLIES

Re: How do you perform formatted string conversion?

Phenom
Phenom

There is no equivalent to your sprintf format specifier.

 

this doesn't mean it's not possible to (re-)format the decimals

 

It means , you have write some code to do the job.

 

 

Betreff: How do you perform formatted string conversion?

Gears Phenom Gears Phenom
Gears Phenom

I wouldn't know that this kind of function is provided out-of-the-box, I guess you have to write that method yourself. How about this as a starting point?

-- f = Format-String: %N.Mx, where 
--                    - N is number of characters before decimal point
--                    - M is number of decimals to display (max/if any)
--                    - x is any string-suffix
param r : real, f : string -> string

var N, M, I : integer

-- check
if (copy(f,1,1) /= "%") then debug; end

-- parse format-string
var P : integer := pos(".",f)
if (p = 0) then
	-- display no decimals
	N := str_to_num(omit(f,1,1))
	M := 0
else		
	N := str_to_num(copy(f,2,P-1))
	M := str_to_num(omit(f,1,P))
end

-- get integer-value
I := floor(r)
result := to_str(I)
while (strlen(result) < N) loop
	result := "0" + result
end

-- add decimals?
if (M > 0) and not (r ~= I) then
	result := result + "." + copy(omit(to_str(round(r, M) - I),1,2),1,M)
end

This would be some results:

5.3, %2.4             ==>05.3
7, %3.3                 ==>007
123.456, %2.2    ==>123.45

 

Regards,
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?

Genius
Genius

Wow Alex - thanks a lot for a prototype sprintf, that is marvellous. And it handles floats too, superb.

I will try that out as soon as I have some discretional time at work. Then post back here.

BTW - I wrote a better version of "%N.Me" format which I use in Matlab quite a bit, but that is far too complex to port to SimTalk I suspect.

Betreff: How do you perform formatted string conversion?

Siemens Phenom Siemens Phenom
Siemens Phenom

Just an addition: If you just want to specifiy the precision, you can use the function num_to_str. The optional second argument is the precision.

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 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)

Betreff: How do you perform formatted string conversion?

Gears Phenom Gears Phenom
Gears Phenom

StrRpos strRcopy should do that job

Edited: I'm not capable of multitasking, it would seem...

____________________________________________________________
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

right could be implemented like this:

param str:string, chars:integer -> string
var len := strlen(str)
return copy(str, len-chars, len)
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?

Gears Phenom Gears Phenom
Gears Phenom

or we could do it with omit Smiley Tongue

param str:string, chars:integer -> string
return omit(str, 1, strlen(str)-chars)

 

____________________________________________________________
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?

Genius
Genius
Thanks Alex and Ralf^1,

Herewith my implemented solution to the original problem using your suggestions above

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

/*
Example : .L.U.SprintfInt( 7, 3) --> 007

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

param intVal:integer, numDigits:integer -> string

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

// AlexD solution
return omit( str, 1, strlen( str) -numDigits)


// Ralf Tobel solution
// var lenStr := strlen(str)
// return copy( str, lenStr -(numDigits-1), lenStr) ^1 : Note that need to use (numDigits-1)

Note ^1 : Ralf - please note you needed a small correction, noted in the code above