Skip to main content
Known Participant
April 21, 2019
Question

Oops on the Math.floor script missed something important

  • April 21, 2019
  • 2 replies
  • 853 views

Hi all,

You helped me tremendously with this script earlier, but I found one error I made I have to halve the first field and use the mathfloor here before continuing operations. I thought it was an easy fix and may be, but I am missing it. This is the change I made.

event.value = Math.floor(Number(this.getField("g1p1resources").valueAsString)  * 0.5 * Number(this.getField("g1p1multiple1").valueAsString)); 

I moved the *.05 from the end to just after the first statement. Running it through the console it says it is right, but the calculation for example for 5*.5*2 comes up 5 when it should be returning 4

5 halved is 2.5 rounded down to 2 * 2 = 4

I am really rusty on my javascript haven't used it in years and trying to get back in is proving to be a bear. Thank you for your help.

This topic has been closed for replies.

2 replies

try67
Community Expert
Community Expert
April 21, 2019

The question is what you want to use the floor function on. Is it just the value of "g1p1resources" times 0.5, or the entire operation (as I understood earlier). If it's the former you need to move the closing parentheses of the floor command before multiplying by the value of "g1p1multiple1".

Known Participant
April 21, 2019

I want to use the math floor on the g1p1resources times ,5. For example if they have 5 it is halved to 2.5 and then rounded down to 2 before the final multiplication which would make it 2 times whatever the multiplier is.

Thanks for your help on this.

try67
Community Expert
Community Expert
April 21, 2019

OK, that was not clear from your original question (by the way, in the future please post follow-ups under the original thread, instead of in a new one). To do it use this code:

event.value = Math.floor(Number(this.getField("g1p1resources").valueAsString) * 0.5) * Number(this.getField("g1p1multiple1").valueAsString);

Bernd Alheit
Community Expert
Community Expert
April 21, 2019

The result of 5*0.5*2 is 5, not 4.