Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
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
Comments
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    
Hi Thanks for your comments.
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

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

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


// AddZeros Method
public string AddZeros(string output)
{
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..,:)
 
Share this answer
 
Comments
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!
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 ?
 
Share this answer
 
Comments
bjay tiamsic 1-Feb-16 18:37pm    
This is perfect!! Thanks! Please help other programmers also.
BillWoodruff 1-Feb-16 21:16pm    
You are welcome.
Try string strResult=strValue.PadRight(N, '0');
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900