Highlighted

How to create a counting number but starting with Zeros

New Here ,
Sep 25, 2019

Copy link to clipboard

Copied

Hello

I want to create a counting number with 6 decimals. I want to get the number 934 896, but I want to start with all decimals in zero like this: 000 000.

I wrote this expression:

t = Math.floor(effect("Control del deslizador")("Deslizador"))
t += "";
t.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1 ");

 

But, I start with a single zero.

 

Thanks ofr your help!!

Adobe Community Professional
Correct answer by Tomas_Sinkunas | Adobe Community Professional

Here is a robust way to do what you are looking for. The method of adding extra characters on left or right of the string is called padding. So here's a function that converts your number into a string and pads it to a given length of 6 characters. In case you need more '0' characters at the start, simply change number 6 to some other value.

 

Cheers.

 

function padStart(string, targetLength, character) {
	// Convert 'string' into a string primitive
	string = (string instanceof String) ? string : string.toString();
	
	// floor if number, or convert non-number to 0;
	targetLength = targetLength >> 0;

	// Assign default padding character
	character = character || ' ';

	while (string.length < targetLength) {
		string = character + string;
	}

	return string;
};

var slider = thisComp.layer('Null 1').effect('Slider Control')('Slider');
var paddedString = padStart(slider.value, 6, '0');

paddedString;

 

Views

590

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

How to create a counting number but starting with Zeros

New Here ,
Sep 25, 2019

Copy link to clipboard

Copied

Hello

I want to create a counting number with 6 decimals. I want to get the number 934 896, but I want to start with all decimals in zero like this: 000 000.

I wrote this expression:

t = Math.floor(effect("Control del deslizador")("Deslizador"))
t += "";
t.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1 ");

 

But, I start with a single zero.

 

Thanks ofr your help!!

Adobe Community Professional
Correct answer by Tomas_Sinkunas | Adobe Community Professional

Here is a robust way to do what you are looking for. The method of adding extra characters on left or right of the string is called padding. So here's a function that converts your number into a string and pads it to a given length of 6 characters. In case you need more '0' characters at the start, simply change number 6 to some other value.

 

Cheers.

 

function padStart(string, targetLength, character) {
	// Convert 'string' into a string primitive
	string = (string instanceof String) ? string : string.toString();
	
	// floor if number, or convert non-number to 0;
	targetLength = targetLength >> 0;

	// Assign default padding character
	character = character || ' ';

	while (string.length < targetLength) {
		string = character + string;
	}

	return string;
};

var slider = thisComp.layer('Null 1').effect('Slider Control')('Slider');
var paddedString = padStart(slider.value, 6, '0');

paddedString;

 

Views

591

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 25, 2019 0
Guide ,
Sep 25, 2019

Copy link to clipboard

Copied

Try this:

 

t = Math.floor(effect("Control del deslizador")("Deslizador"));
n = 6 - t.toString().length;
"00000".substr(0,n) + t

 

 

Dan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 25, 2019 2
New Here ,
Sep 27, 2019

Copy link to clipboard

Copied

Hi Dan!

Your answer really helped me, although I need a space in the middle of the number like this: 000 000

With your expression I got this: 000000

I tried to mix your expression with mine, but it didn't work.

Could you help me with this issue?

 

Thanks a lot!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 27, 2019 0
Adobe Community Professional ,
Sep 27, 2019

Copy link to clipboard

Copied

Here's how you do it: split the string into an array of N size items (chunks) and join the result with a separator. Simply change stringchunkSize and separator variables to your needs.

 

Given a string 123456789 with a chunkSize of value 3 and a separator as - the result would be 123-456-789

 

function splitStringIntoChunks(string, chunkSize) {
	var chunks, pattern;

	// Convert 'string' into a string primitive
	string = (string instanceof String) ? string : string.toString();

	// Floor the size
	chunkSize = Math.floor(chunkSize);

	// Build RegExp pattern
	pattern = new RegExp('.{0,' + chunkSize + '}', 'g');

	// Get array of matches
	chunks = string.match(pattern);

	return chunks;
}

var string = 123456789;
var chunkSize = 3;
var separator = ' ';

var chunks = splitStringIntoChunks(string, chunkSize);
var result = chunks.join(separator);

result;

 

 

 

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 27, 2019 0
Adobe Community Professional ,
Sep 26, 2019

Copy link to clipboard

Copied

Here is a robust way to do what you are looking for. The method of adding extra characters on left or right of the string is called padding. So here's a function that converts your number into a string and pads it to a given length of 6 characters. In case you need more '0' characters at the start, simply change number 6 to some other value.

 

Cheers.

 

function padStart(string, targetLength, character) {
	// Convert 'string' into a string primitive
	string = (string instanceof String) ? string : string.toString();
	
	// floor if number, or convert non-number to 0;
	targetLength = targetLength >> 0;

	// Assign default padding character
	character = character || ' ';

	while (string.length < targetLength) {
		string = character + string;
	}

	return string;
};

var slider = thisComp.layer('Null 1').effect('Slider Control')('Slider');
var paddedString = padStart(slider.value, 6, '0');

paddedString;

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 26, 2019 1
Sara5FA5 LATEST
New Here ,
Sep 29, 2020

Copy link to clipboard

Copied

Hi! Thank's for this! 

I have a problem with decimals though, I don't want them. How do I get rid of them? 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 29, 2020 0