Jump to content
Sign in to follow this  
M1ke_SK

fill up 0 to number ( "1" => "001")

Recommended Posts

I have number as string. I want to add zeroes to number.

 

Example:

"1" => "001",
"11" => "011"

 

Any slick function for that? Possibly without some loop.

 

_number = "11";
for "_i" from (count _number) to 2 do 
{
	_number = "0" + _number;
};
 

Share this post


Link to post
Share on other sites

Why no loop though?

TAG_fnc_fillNumber = {

	params [["_digits",3],["_number","1"]];

	_count = count _number; 
	_difference = _digits - _count; 

	if (_count < _digits) then { 
	 
		_toAdd = ""; 

		for "_i" from 1 to _difference do { 

			_toAdd = _toAdd + "0"; 

		}; 

		(_toAdd + _number)

	}; 
};
_test = [3,"1"] call TAG_fnc_fillNumber;
systemchat str _test; // "001"
_test = [6,"11"] call TAG_fnc_fillNumber;
systemchat str _test; // "000011"

 

Cheers

Share this post


Link to post
Share on other sites

Maybe some solution: Add x number of "0" before number and trim last 3 digits.

Example:

"11" > "000000000011" > "011"

 

Share this post


Link to post
Share on other sites

How would that be any different from my solution?

Anything wrong about for "_i" that I don't know about?

Does it not do what you requested in the first post?

 

Cheers

Share this post


Link to post
Share on other sites

_paddingZeroes = "0000000000";

_formatTotalLength = 5;

_number = "123";

 

_result = (_paddingZeroes select [0, _formatTotalLength - count _number]) + _number; // "00123"

  • Like 2

Share this post


Link to post
Share on other sites
15 minutes ago, killzone_kid said:

_paddingZeroes = "0000000000";

_formatTotalLength = 5;

_number = "123";

 

_result = (_paddingZeroes select [0, _formatTotalLength - count _number]) + _number; // "00123"

 

Yes, that I was talking about. The mighty select function :D

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×