Skip to main content
ryanh81819492
Participant
September 14, 2019
Question

How do I create a 5-digit slider expression? (It's for a lab test LCD screen animation.)

  • September 14, 2019
  • 2 replies
  • 519 views

I know how to control the digits after the decimal using value.toFixed; however, I want to start with +00000.0 and end with the number I choose (eg: +01234.5). Right now my slider starts with +00.0 and ends with +01234.5. Does anyone know if this can be fixed with Effect Controls or using the Expression (JavaScript)?

 

Here's my current Expression: "+0"+ effect("Slider Control")("Slider").value.toFixed(1)

    This topic has been closed for replies.

    2 replies

    Tomas Sinkunas
    Legend
    September 15, 2019

    Here's a cleaner way to achieve what you are after:

    function pad(string, length, character) {
    	// Make sure string is String
    	string = (typeof string === 'string' || string instanceof String) ? string : string.toString();
    
    	// If not defined, set padding length to 2
    	length = length || 2;
    
    	// If not defined, set padding character to '0'
    	character = character || '0';
    
    	while (string.length < length) {
    		string = character + string;
    	}
    
    	return string;
    }
    
    var value = effect("Slider Control")("Slider").value;
    
    // Round value to 1 decimal place
    var valueAsString = value.toFixed(1);
    
    // Pad string to total of 6 characters
    var paddedString = pad(valueAsString, 6, '0');
    
    // Add customn prefix
    var result = '+0' + paddedString;
    
    result;

     

     

    Martin_Ritter
    Legend
    September 15, 2019
    The while-loop is a nice idea!
    Tomas Sinkunas
    Legend
    September 15, 2019
    Thanks. Also this is a more modular way to do things, since you can just copy/paste the function to another expression/script to pad the strings.
    Martin_Ritter
    Legend
    September 14, 2019

    Not elegant, but you can follow the same logic already applied:

    sliderRaw = effect("Slider Control")("Slider").value.toFixed(1);
    
    if (sliderRaw < 10){
    slider = "+0000"+ sliderRaw;}
    
    if (sliderRaw >= 10 && sliderRaw < 100){
    slider = "+000"+ sliderRaw;}
    
    if (sliderRaw >= 100 && sliderRaw < 1000){
    slider = "+00"+ sliderRaw;}
    
    if (sliderRaw >= 1000 && sliderRaw < 10000){
    slider = "+0"+ sliderRaw;}
    
    if (sliderRaw >= 10000 && sliderRaw < 100000){
    slider = "+"+ sliderRaw;}
    
    slider;

     

    *Martin

     

    ryanh81819492
    Participant
    September 15, 2019
    Martin!!! This worked perfectly. So happy. What a neat fix. Very clever. This was a huge deal for me. Am I allowed to send you a tip for this??
    Martin_Ritter
    Legend
    September 15, 2019
    Glad to hear - have fun with it. And no tip please - this is a voluntary forum.