Skip to main content
Known Participant
Apr 21, 2019 3:46 PM UTC
Question

Oops on the Math.floor script missed something important

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 is closed to new replies. Start a new post to keep the conversation going.

5 replies

try67
Community Expert
Community Expert
Apr 21, 2019 4:30 PM UTC

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".

Translate
Known Participant
Apr 21, 2019 4:40 PM UTC

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.

Translate
try67
Community Expert
Community Expert
Apr 21, 2019 4:44 PM UTC

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);

Translate
Bernd Alheit
Community Expert
Community Expert
Apr 21, 2019 4:25 PM UTC

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

Translate