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

New Here ,
Dec 29, 2016

Copy link to clipboard

Copied

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.

TOPICS
Acrobat SDK and JavaScript

Views

674

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Dec 30, 2016

Copy link to clipboard

Copied

What JavaScript code do you use?

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Dec 30, 2016

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Most Valuable Participant ,
Dec 31, 2016

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jan 02, 2017

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Most Valuable Participant ,
Jan 08, 2017

Copy link to clipboard

Copied

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 ...

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Most Valuable Participant ,
Jan 08, 2017

Copy link to clipboard

Copied

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 ;}

}

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jan 08, 2017

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jan 09, 2017

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more