Reputation: 4268
I am trying to represent an integer in 12 digit format, 10 digit format etc with leading zereos. I have used this:-
DecimalFormat decimalFormat = new DecimalFormat("000000000000");
String invoiceAmountString = decimalFormat.format(IntegerValue);
I want a generic function such that when i pass 10 or any other value it should use that number of zereos in DecimalFormat. I have an idea like this;-
case 10:
DecimalFormat decimalFormat = new DecimalFormat("0000000000"); // 10 digit
case 12:
DecimalFormat decimalFormat = new DecimalFormat("000000000000"); // 12 digit
but decimal format can go from 5-25 digit. I need a function like:-
convertToFormat(Integer amount, Integer noOfDigits)
such that if i pass 5,10,15 or whatever value in noOfDigits
it should use that number of zereos in DecimalFormat.
Upvotes: 2
Views: 138
Reputation: 136102
try this
String s = String.format("%0" + numberOfDigits + "d", amount);
Upvotes: 2