www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - dynamic format specifier possible?

reply forkit <forkit gmail.com> writes:
I would like to calculate the width of the format specifier 
dynamically, at runtime.

e.g int WIDTH = something.length;

then my format specifier would be: %WIDTHs instead of %9s

// ---

module test;

import std;

void main()
{
     int val = 999000;
     writefln("[%9s]", val); // [   999000]
}

// ---
Jan 23 2022
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 1/23/22 13:59, forkit wrote:
 I would like to calculate the width of the format specifier dynamically, 
 at runtime.
You use an asterisk and provide the width as an argument. This one uses the length of the name of the program: import std; void main(string[] args) { int val = 999000; writefln("[%*s]", args.front.length, val); // [ 999000] } Ali
Jan 23 2022
parent forkit <forkit gmail.com> writes:
On Sunday, 23 January 2022 at 22:08:28 UTC, Ali Çehreli wrote:
 You use an asterisk and provide the width as an argument. This 
 one uses the length of the name of the program:

 import std;

 void main(string[] args)
 {
   int val = 999000;
   writefln("[%*s]", args.front.length, val); // [   999000]
 }

 Ali
perfect! thanks.
Jan 23 2022