15,998,003 members
See more:
Hi! I am generating a text file that needs zero as a filler if the string does not meet the required length. The field that I need is the amount to I first convert it to string, but Im having a hard time getting the logic to meet my requirement.

I have sample amount: 34340
the string should consist of 18 characters and if the amount is less than a hundred thousand it should have 1 leading zero, it should be: 034340000000000000, therefore this format has 12 zeros after the actual amount

so if the amount is greater than or equal to 100,000, the format should be (eg143554) 143554000000000000

and if the amount is 1 million or above, the amount should consist of 11 zeros after the actual amount.

Any help I would really appreciate! Thanks!
Posted
dan!sh 1-Feb-16 2:31am
Why are you doing this? If at any point of time you need to convert this string to number, how would you know what the actual number was?
BillWoodruff 1-Feb-16 3:14am
Are you going to be using this transformation frequently ?

Once you've done this, how will you ever know that "100000" which has bee padded with 12 '0's is not the same as "1000000" padded with eleven zeroes ? You are going to lose information: depending on what you are doing that may, or may not, be critical.
bjay tiamsic 1-Feb-16 18:26pm
Yes I will be needing this frequently, I will be using this as a text file needed to submit to a government branch for government contribution, the file needs to follow this requirement.
So I dont think it matters to convert the string back to number

## Solution 2

long input = 34340;
stringbuilder output = input.tostring; // somehow convert int to string or builder as per req

if(input<100000)
{
output = '0' + output; // concatination
}
else if(input>100000 and input<1000000)
{
}
else
{
output = output + "00000000000";
}

{
if(output<18)
{
int i = 18 - output;
for(loop over i)
{
output = output + '0'; // concatinate
}
}
return output;
}

Above method is bit lenghty but gives u any idea to find a solution.

Happy Coding..,:)

AnvisNet 1-Feb-16 4:50am
Yes as stated by BillWoodruff use padright that makes above code more easy.
bjay tiamsic 1-Feb-16 18:28pm
Thank you. I have thought of this logic, I am just wondering if there are built-in functionality. Anyway, thank you so much for this. Will try!

## Solution 3

C#
```private string PadZero(int source, int limit = 18)
{
StringBuilder sb = new StringBuilder(source.ToString().PadRight(limit, '0'));

if (source < 100000) sb.Insert(0, '0');

// resize to final sie
sb.Length = limit;

return sb.ToString();
}```
Notes:

1. why use 'StringBuilder ? to reduce the creation of new strings, and keep memory use lower, as well as (probably) improve performance.

2. note the "lazy" strategy here: we pad the incoming number-turned-to-string with lots of zeroes, and we throw away the ones we don't need to return the result ... rather than calculate the required amount of padding.

I've tested this with all three of your input values, and get the expected result, but you should test this code further.

Do you need to handle negative numbers ? Do you an integer zero to be transformed to 18 char '0's ?

bjay tiamsic 1-Feb-16 18:37pm
BillWoodruff 1-Feb-16 21:16pm
You are welcome.

## Solution 1

Try `string strResult=strValue.PadRight(N, '0');`

v2