Skip to main content
Participating Frequently
October 12, 2017
Question

Ignore Last Digit

  • October 12, 2017
  • 1 reply
  • 340 views

I have two fields where information is entered. When the numbers are calculated, I would like for the last digit of each entry to not be included. How do I accomplish this?

Entry 1 is 1.23452

Entry 2 is 1.23437

Should be calculated in a new field as 1.2345 - 1.2343 (the two and the 7 are dropped off, respectively).

This topic has been closed for replies.

1 reply

Inspiring
October 12, 2017

One could convert the values to a string and then sub-string the string value to remove the last digit or use the Math.floor to remove the last digit when multiplied by 10,000 and dividing the result by 10,000 to restore the decimal point to the original position.

editbdAuthor
Participating Frequently
October 12, 2017

This is the code I am currently using to display the result for:

Should be calculated in a new field as 1.2345 - 1.2343 (the two and the 7 are dropped off, respectively).

Where Entry Sunday1 is 1.23452

Where Exit Sunday1 is 1.23437

This code drops the last digit and works correctly if the last digit is 5 numbers below (for example if the last digit "7" was a "6").

var BuySell = this.getField("BuySell Sunday1").valueAsString;

var val = this.getField("Entry Sunday1").valueAsString;

var countDecimals = function(value) {

    if (Math.floor(value) !== value)

        return value.toString().split(".")[1].length || 0;

    return 0;

}

countDecimals(val);

if(BuySell == "Buy")event.value = ( this.getField("Exit Sunday1").value - this.getField("Entry Sunday1").value );

if(BuySell == "Sell")event.value = ( this.getField("Entry Sunday1").value - this.getField("Exit Sunday1").value );

if (countDecimals(val) == "3")event.value =  event.value("Exit Sunday1").value* "100";

if (countDecimals(val) == "4")event.value = event.value("Exit Sunday1").value * "1000";

if (countDecimals(val) == "5")event.value = event.value * "10000";

if(event.value >= 0)  

    event.target.textColor = color.blue;

else    

    event.target.textColor = color.red;

event.value = parseFloat(event.value).toFixed(0);