So my problem was such that I had to compute 10^n such that n ~ 10^5. Obviously it wouldn't fit in any data type hence I decided to use a string instead. Finally, I did find the solution in the beginners book https://beginnersbook.com/2014/07/java-right-padding-a-string-with-spaces-and-zeros/.
I don't want the BigInteger
solution of multiplying 10 n times.
public class PadRightExample2 {
public static void main(String[] argv) {
System.out.println("#" + rightPadZeros("mystring", 10) + "@");
System.out.println("#" + rightPadZeros("mystring", 15) + "@");
System.out.println("#" + rightPadZeros("mystring", 20) + "@");
}
public static String rightPadZeros(String str, int num) {
return String.format("%1$-" + num + "s", str).replace(' ', '0');
}
}
Output:
#mystring00@
#mystring0000000@
#mystring000000000000@
Can anybody explain what is %1$-
and what is s
used for ?