Skip to main content
Participating Frequently
May 23, 2022
Resuelto

Why can't Captivate Multiply?

  • May 23, 2022
  • 2 respuestas
  • 518 visualizaciones

As I try to turn decimals into a percent by multiplying decimals by 100, I get this funkiness. 

 

Expression = var_a * 100

var_a = .07

 

should give me 7 correct? Instead it gives me this...

 

WHY??????????

 

    Este tema ha sido cerrado para respuestas.
    Mejor respuesta de TLCMediaDesign

    Technically is better to use Math.round instead of toFixed(0) as toFixed returns a string and you could not do any further calculations with the result of toFixed.

     

    Also, it has nothing to do with Captivate being funky, it is JavaScript that cannot accurately represent certain numbers in floating point binary, so you get those results. 

     

    So with JavaScript just add this to your action:

     

    Since you do not show what variable is being set with the result of your expression, I'll just use the variable "result"

    Math.round(window.result);

    2 respuestas

    TLCMediaDesign
    Inspiring
    May 24, 2022

    Technically is better to use Math.round instead of toFixed(0) as toFixed returns a string and you could not do any further calculations with the result of toFixed.

     

    Also, it has nothing to do with Captivate being funky, it is JavaScript that cannot accurately represent certain numbers in floating point binary, so you get those results. 

     

    So with JavaScript just add this to your action:

     

    Since you do not show what variable is being set with the result of your expression, I'll just use the variable "result"

    Math.round(window.result);

    Lilybiri
    Legend
    May 24, 2022

    Thanks, David. I took this from an example where no further calculations were needed, since it was an end result. OP used the variable var_a.

    Participating Frequently
    September 26, 2022

    Thank you both! I ended up using javascript toFixed(0) because it was just a display issue. I will use Math.Round when I need do more math to it.

    Lilybiri
    Legend
    May 24, 2022

    You cannot format the number of decimals in Captivate, you need to do that using JS. I have some examples here:

    http://blog.lilybiri.com/percentage-progress-indicator-non-linear-course-solution-1

    In your case you could choose to add those lines to your advanced action:

    var perc = window.cpAPIInterface.getVariableValue(“var_a”);  window.cpAPIInterface.setVariableValue(“var_a”,toFixed(0));

    Participating Frequently
    September 26, 2022

    I ended up writing it like this:
    window.cpAPIInterface.setVariableValue("var_bonusVolumeFormat", var_bonusVolume.toFixed(0));
    But your way might be easier to understand.