Skip to main content
December 29, 2016
Question

How to store fraction value in text field and then convert it to decimal

  • December 29, 2016
  • 2 replies
  • 2114 views

I have a form where I need to store text field value as a fraction (like 2 1/8) and then convert to decimal. I tried to use custom calculation through javascript but some how it is not taking fraction value and converting it to decimal.Please help.

This topic has been closed for replies.

2 replies

Inspiring
January 8, 2017

First let us look at what your input value could look like.

A whole number like 64 or "1)".

A fraction string like  "1/4".

A whole number plus a fraction string like "10 3/4" or "10-3/4".

With so many possible input variations I would look at using the Regular Expression object to identify which variation of the various inputs I have to process and then do the math. I would also use the "switch" statement so I only run the one conversion process that is needed and skip the other 2 options. I would also include a way to deal with a bad input value.

A possible function:

function ConvertFrac2Dec(cString)
{
// Convert a string of numberic values consisting of a whole number and fraciton string to a decimal number;
// invalid input will result in a return of a null value;

// convert numeric string with a fraction to a decimal number;
var nDecimal = null;
switch(true) {
  case /^(\d*)$/.test(cString) :
   nDecimal = RegExp.$1;
  break;

  case /^(\d*)\/(\d*)$/.test(cString):
   nDecimal = RegExp.$1 / RegExp.$2
  break;

  case /^(\d*)[ -](\d*)\/(\d*)$/.test(cString):
   nDecimal = Number(RegExp.$1) + Number((RegExp.$2 / RegExp.$3));
  break;

  default:
   // all other strings;
   nDecimal = null
  break;
}

return nDecimal;
} // end ConvertFrac2Dec function;

The custom calculation for the result field could be:

var nResult = ConvertFrac2Dec(this.getField("FractionString").value);

if(nResult == null)

{

app.alert("Invlid input for conversion from fractional string to decimal value", 1, 0);

event.vaue = "";

} else {

event.value = nResult;

}

You will have to adjust the input field name as needed.

Inspiring
January 9, 2017

See  Fraction to Decimal for a working example of the script. It also include validation of the input and a sample calculation using fractional values.

Bernd Alheit
Community Expert
Community Expert
December 30, 2016

What JavaScript code do you use?

December 30, 2016

Hello Bernd,I have used below code-

So field's formatting I am setting as none and then I enter value (say 2 1/4) as a fraction in it.I am splitting this value based on space and then getting all value as you can see, But when I enter value in field I don't get any thing converted in decimal.

var wholeNum = string(this.getField("fieldRow1").value).split(' ')[0]var fractionValue = string(this.getField("fieldRow1").value).split(' ')[1]

var fracNumerator = string(fractionValue ).split('/')[0]var fracDeno = string(fractionValue ).split('/')[1]var decimalVal = (wholeNum * fracDeno  + fracNumerator )/fracDeno if (!isFinite(decimalVal ) || isNaN(decimalVal )) {    event.value = "";} else {event.value = decimalVal ;}

Please let me know if you have any solution for this.

Thanks,Manisha

Legend
December 31, 2016

Some ideas for debugging your program. Use app.alert to check the values of your four variables after splitting. Check the console for errors.