StringFormat & PrintFormat

 

Hello,

does anyone know how to store the number of 'integers' of a double value including leading zeros in a string variable?

This is how it works with the decimal places:

int digs = 2 ;
string fmt = StringFormat ( "%%.%df" ,digs);
string strVal = StringFormat (fmt, 9.123456678 );

This gives: 9.12.

Now do I'd like to get: 09.12?

Google finds (printf) a thousand times only the basic version, F1 (I use that synonymous;) offers at least the above solution.

Unfortunately I only get 9.12 with this experimental tries:

fmt = StringFormat ( "%%%s.%df" , StringFormat ( "%0i" , 3 +digs),digs); // 2 pre-decimal places + decimal point + digs = 5 places in my example

Everything else tried (paying around with %%)  results in nothing meaningful :(

Even this prints only 9:12:

 string fmt = StringFormat ("%%%s", StringFormat ("% 0 i.% if ", 3 +digs,digs));
 
Carl Schreiber:

Now do I'd like to get: 09.12?


double x = 9.12;
string y = StringFormat("%05.2f", x);
Print(y);
 
Carl Schreiber:

Hello,

does anyone know how to store the number of 'integers' of a double value including leading zeros in a string variable?

This is how it works with the decimal places:

This gives: 9.12.

Now do I'd like to get: 09.12?

Google finds (printf) a thousand times only the basic version, F1 (I use that synonymous;) offers at least the above solution.

Unfortunately I only get 9.12 with this experimental tries:

Everything else tried (paying around with %%)  results in nothing meaningful :(

Even this prints only 9:12:

Its in the documentation: %[flags][width][.precision][{h | l | ll | I32 | I64}]type

0 (zero)

Zeroes are added before an output value within the preset width. If 0 flag is specified with an integer format (i, u, x, X, o, d) and accuracy specification is set (for example, %04.d), then 0 is ignored.

Nothing is added


Examples:

PrintFormat( "[%06.2f]", 9.123456678 ); // -> [009.12]
PrintFormat( "[%06.3f]", 9.123456678 ); // -> [09.123]
PrintFormat( "[%05.3f]", 9.123456678 ); // -> [9.123]
PrintFormat( "[%05.2f]", 9.123456678 ); // -> [09.12]
 

This I know and this is not what I am looking for!

I need to define the string variable fmt (s.a.).

The number of digits (int digs) is set from 'outside' now the total number of places has to be adjusted - see the comment of my example.

Your examples explicitly requires to know the number of digits and places at compile time and is not adjustable by the program!

 
Carl Schreiber:

This I know and this is not what I am looking for!

I need to define the string variable fmt (s.a.).

The number of digits (int digs) is set from 'outside' now the total number of places has to be adjusted - see the comment of my example.

Your examples explicitly requires to know the number of digits and places at compile time and is not adjustable by the program!

@Carl Schreiber, the examples are to show you how the formatting works. Surely you can figure out how to adapt it, right?

But if you want to be spoon-fed, here it is:

int digs = 2, ints = 2;
string fmt = StringFormat( "%%0%d.%df", ints+digs+1, digs ); // -> "%05.2f"
string strVal = StringFormat( fmt, 9.123456678 ); // -> "09.12"
 
Fernando Carreiro:

@Carl Schreiber, the examples are to show you how the formating works. Surely you can figure out how to adapt it, right?

But if you want to be spoon-fed, here it is:

You could have just said you misunderstood Carl's request.

If he is asking that's because he didn't know how to adapt it.

 
Alain Verleyen: You could have just said you misunderstood Carl's request. If he is asking that's because he didn't know how to adapt it.

He is not a newbie and already has some good coding skills and knowledge under his belt! If I look at his code and he was able to dynamically set the precision ("digs"), then it stands to reason that he can apply the same logic to the width as well, once it is explained to him how the width works.

 
Fernando Carreiro:

He is not a newbie and already has some good coding skills and knowledge under his belt! If I look at his code and he was able to dynamically set the precision ("digs"), then it stands to reason that he can apply the same logic to the width as well, once it is explained to him how the width works.

I am just suggesting you to answer in a more friendly way.

 

try to use StringLen and StringFind

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   double x=9.123456678;
   int    left=leftDigits(x);
   int    right=rightDigits(x);
   
   Print("x=",(string)x,"; leftDigits=",left,"; rightDigits=",right);
  }
//+------------------------------------------------------------------+

int rightDigits(double v)
{
   string s=(string)v;
   int p=StringFind(s,".",0);
   int l=StringLen(s);
   return((p>0)?l-p-1:0);
}


int leftDigits(double v)
{
   string s=(string)v;
   int p=StringFind(s,".",0);
   return((p>0)?p:StringLen(s));
}
 

Thank you!

This working solution is nearly what I was trying first:

string fmt = StringFormat( "%%%0d.%df", ints+digs+1, digs ); // wrong :(