Strange String manipulation method
My colleague was trying to write a method (In Java/C#) that would append
an arbitrary number of zero's to the end of a String. However, I can't
seem to figure out what his approach is.
This is the Java code, the C# is basically equivalent:
String appendzeros(int input, int no_of_digits_required)
{
String result = Integer.toString(input);
int i,j;
for(i = 10, j = 1; i <= Math.pow(10, no_of_digits_required-1); i =
i*10, j++)
{
if(input / i == 0)
{
for(int k = 1; k <= no_of_digits_required-j; k++)
result = "0" + result;
break;
}
}
return result;
}
Any ideas?
No comments:
Post a Comment