8 Replies Latest reply on Jan 9, 2017 1:20 PM by gkaiseril

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

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.

• ###### 1. Re: How to store fraction value in text field and then convert it to decimal

What JavaScript code do you use?

• ###### 2. Re: How to store fraction value in text field and then convert it to decimal

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

• ###### 3. Re: How to store fraction value in text field and then convert it to decimal

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

• ###### 4. Re: How to store fraction value in text field and then convert it to decimal

There are missing ; characters at the end of the statements.

• ###### 5. Re: How to store fraction value in text field and then convert it to decimal

There's no built-in "string" method in JS. Instead, just use the valueAsString property of the fields you're accessing, or the .toString() method, or even cast your variable to a string like this: ""+myVariable ...

• ###### 6. Re: How to store fraction value in text field and then convert it to decimal

I fixed your code and made some adjustments to handle an empty string or a number without a fraction correctly. It's not perfect but it's better than what you had and it should work...

```var v = this.getField("fieldRow1").valueAsString;
var parts = v.split(" ");
if (parts.length<=1) event.value = v;
else {
var wholeNum = Number(v.split(' ')[0]);
var fractionValue = v.split(' ')[1];
var fracNumerator = Number(fractionValue.split('/')[0]);
var fracDeno = Number(fractionValue.split('/')[1]);
var decimalVal = ((wholeNum * fracDeno) + fracNumerator) / fracDeno;
if (!isFinite(decimalVal) || isNaN(decimalVal)) {event.value = "";} else {event.value = decimalVal ;}
}
```
• ###### 7. Re: How to store fraction value in text field and then convert it to decimal

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.