Case 1: When value of “gross_total” field is 0, then the value of “overtime_percent” is shown as INFINITY (obviously since its a divide by 0), but for this case I want to set the value of “overtime_percent” as simply 0 instead of INFINITY

Case 2: When value of “gross_total” field is negative (-10, -25, etc.) then the value of “overtime_percent” is shown as a negative value, but I want to show the value of “overtime_percent” to always show as a positive number.
For eg.:
overtime_total: 50
gross_total: -200
overtime_percent: -25 (this should be shown as 25 instead of -25)

frm.set_value(“overtime_percent”, Math.abs (flt(frm.doc.overtime_total) / flt(frm.doc.gross_total) * 100));
You can use abs method to show positive number.

so before setting value convert gross_total into positive number then set the value for overtime_percent.
Math.abs() should have worked, what error did you get?

The field “gross_total” is shown on the form so it should be correctly shown as a negative figure. Only the “overtime_percent” field should be calculated and shown as positive.

If I convert the “gross_total” to positive before setting value of “overtime_percent” then it wrongly shows gross total field as a positive even if the user has put in a negative value there.

Tired using abs, fabs, fabsf, math.abs but the “overtime_percent” field does not calculate at all if any of these is used.