Skip to main content
Known Participant
May 26, 2016
Answered

Calculation result separated in whole number and decimal, not rounding correctly

  • May 26, 2016
  • 2 replies
  • 1280 views

I have 2 fields for the result of a division: the whole number and the decimal (1 decimal). I Math.floor the result to get the whole number and all goes well until I hit a case where the result is 92.97.

This should give "93" in the whole field and "0" in the decimal field, instead it gives me "92" and "10" (even though I only allow 1 character in the field).

How do I get the "93" and "0" in the following code?

fieldLVEDV = this.getField("LVEDV").value;

fieldBSA = this.getField("BSA").value;

if (fieldLVEDV != "" && fieldBSA != ""){

totalLVEDV = this.getField("LVEDV").value + ((this.getField("LVEDVDec").value)/10);

calculation = (totalLVEDV / fieldBSA);

calculationDecimal = calculation;

partWhole = Math.floor(calculation);

if(partWhole <0) {partWhole ="";}

this.getField("Index").value = partWhole ;

this.getField("IndexDecimal").value = Math.round(10*(calculationDecimal-partWhole));

{if (this.getField("IndexDecimal").value = "10")

{

this.getField("Index").value = partWhole+1;

this.getField("IndexDecimal").value = "0"

}}

} else {

    this.getField("Index").value = "";

    this.getField("IndexDecimal").value = ""; 

}

I thought adding the blue part would do the job but obviously it didn't work.

This topic has been closed for replies.
Correct answer Karl Heinz Kremer

Do you get 92.7 (178/1.92) before entering the 5 in the decimal field to make it 178.5?

If I remove the blue script, I would get 92.7 for 178 (correct answer), and 92.1 for 178.5

With the blue script, I get 93.0 for both 178 and 178.5.


Ah, your problem is that you are not using the correct comparison operator. To check if two values are equal, the operator is "==" an not not "=". By using "=", you are assigning the value 10 to the field, which you are then changing to 0 a couple of lines down. Use "==" and things should work correctly.

2 replies

Inspiring
May 26, 2016

I would use the util.printf() method to round the result of the division and then the modulo operator to get the decimal fraction.

fieldLVEDV = this.getField("LVEDV").value;
fieldBSA = this.getField("BSA").value;
if (fieldLVEDV != "" && fieldBSA != ""){
// perform the divison and roud the result;
// calculation = (totalLVEDV / fieldBSA) rounded to 1 decimal place;
totalLVEDV = util.printf("%,1 1.1f", fieldLVEDV / fieldBSA);
console.println(totalLVEDV);
// get the whole number protion of the result;
var partWhole = Math.floor(totalLVEDV);
var partDecimal = Math.floor((totalLVEDV % 1) * 10); // remainder when divided by 1;

console.println( partWhole + " " + partDecimal);
}

Karl Heinz  Kremer
Community Expert
Community Expert
May 26, 2016

What data do you have in your input fields (LVEDV, LVEDVDec and BSA) for the problem to occur?

yeungyamAuthor
Known Participant
May 26, 2016

LVEDV: 178

LVEDVDec: 5

BSA: 1.92

Karl Heinz  Kremer
Community Expert
Community Expert
May 26, 2016

It looks like you are doing the right thing with adjusting the partWhole variable by one if your number of decimals is 10. It also gives me the correct output when I plug that into a simple form.

What application are you using when the result is incorrect?