Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Converting a decimal number to feet and inches  (Read 708 times)

EphPhoenix

  • Newbie
  • *
  • Karma: +0/-0
  • Posts: 2
Converting a decimal number to feet and inches
« on: March 31, 2023, 09:41:43 AM »

Hello!

Idk if anyone else has been hitting their head against a wall on this, but I managed to figure out the coding for it, so I thought I'd share. In the case of this, I have in the Calculate section of the Form Field Properties doing a simple math problem of subtracting one field from another. The coding for feet and inches conversion will go in the Format section in the Custom Format Script. The "event.value" described in the coding is the calculation  from the Calculate section:

var realFeet = event.value
var feet = Math.floor(realFeet);
var inches = Math.round((realFeet - feet) *12) ;
var text = feet + "' " + inches + '" ';
if (event.value) event.value = text


Hope this helps someone! :3

ETA: Have an issue where if the inches = 12, it'll display it as, say 3' 12", as opposed to 4' 0". Working on a fix, and thought I had it by adding an if function, but now it just converts everything to 0 and rounds up. When I find it, I'll post it.

Fix found! Enjoy:

var realFeet = event.value
var feet = Math.floor(realFeet);
var inches = Math.round((realFeet - feet) *12) ;
if (inches < 12) {
var text = feet + "'-" + inches + '" ';
} else {
 var text = (feet+1) + "'-" + 0 + '" ';
}
if (event.value) {
 event.value = text;
}
« Last Edit: April 05, 2023, 04:20:42 PM by EphPhoenix »
Logged