Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Given a string strTotal (all numerical values).

 

I need a mechanism to do the following.

1- Determine the number of digits in strTotal

2- If there are less then 2 (digits 0 to 9) then pad with a 0 to form 2-Digit Values like 00, 01, 02, 03,�, 08, 09.

Else leave as-is.

 

Any clues?

Posted
Given a string strTotal (all numerical values).

 

I need a mechanism to do the following.

1- Determine the number of digits in strTotal

2- If there are less then 2 (digits 0 to 9) then pad with a 0 to form 2-Digit Values like 00, 01, 02, 03,�, 08, 09.

Else leave as-is.

 

Any clues?

 

Something like:

 

public string PaddedNumber(object oOriginal)

{

if (oOriginal.ToString().Length == 1)

{

return oOriginal.ToString().PadLeft(1, '0');

}

else

return oOriginal.ToString();

}

  • 4 years later...
Posted
Originally Posted by Shaitan00

Given a string strTotal (all numerical values).

 

I need a mechanism to do the following.

1- Determine the number of digits in strTotal

2- If there are less then 2 (digits 0 to 9) then pad with a 0 to form 2-Digit Values like 00, 01, 02, 03,�, 08, 09.

Else leave as-is.

 

Any clues?

 

Try this:

 

for (int i = 0; i < strTotal.Length; i++)

{

if (i < 10)

{

Response.Write("0" + strTotal);

}

else

{

Response.Write(strTotal);

}

}

  • Leaders
Posted
I personally would use PadLeft, like Jaco, but the first parameter should be a two (for a total minimum of two characters), not a one, and there is no need to check the length of the string. All of this is assuming that your string contains a single, properly formatted integer.
[sIGPIC]e[/sIGPIC]
Posted

The algorithm that doesn't....

 

Algorithm's algorithm isn't just inferior, it is completely useless.

 

If strTotal is "1", we get:

01

 

If strTotal is "12", we get:

012012

 

 

If strTotal is "1234567890", we get:

0123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678901234567890

 

:rolleyes:

Never trouble another for what you can do for yourself.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...