Quantcast
Channel: User Federico klez Culloca - Stack Overflow
Viewing all articles
Browse latest Browse all 516

Answer by Federico klez Culloca for Printf in Java with variable spaces before the output

$
0
0

The format string is still a string, so assuming a width variable System.out.printf("%" + width +"d", x); does the trick.

So for example

var width = 10; var x = 123;System.out.printf("%" + width +"d", x);

prints 123 (7 leading spaces + 3 digits = 10), while

var width = 3; var x = 123;System.out.printf("%" + width +"d", x);

prints 123


Viewing all articles
Browse latest Browse all 516

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>