• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Finding decimal placement in a number field

Explorer ,
Jan 29, 2018 Jan 29, 2018

Copy link to clipboard

Copied

Hi,

I am having a number field ("Number Field") with decimals and I want to know the placement of decimals, it would be dynamic. Sometimes "Number Field" value will be

("52.6531", "526.531", "5265.31") I want to know the exact place of where the decimal starts like (3, 4, 5) answers for the given values.

TOPICS
Acrobat SDK and JavaScript

Views

610

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , Jan 29, 2018 Jan 29, 2018

Sure, there are various ways. Here's one:

var s = "52.6531"

var indexOfPeriod = s.indexOf(".");

var decimalPart = indexOfPeriod==-1 ? "" : s.substr(indexOfPeriod+1);

Votes

Translate

Translate
Community Expert ,
Jan 29, 2018 Jan 29, 2018

Copy link to clipboard

Copied

You can use the indexOf method, like this:

"52.6531".indexOf(".")

It returns 2 (the index numbers are zero-based).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 29, 2018 Jan 29, 2018

Copy link to clipboard

Copied

Thank you much.

After finding "2" is there any option to get the values after decimal to end and paste to another text field.

Example: "6531" I need to get this answer.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 29, 2018 Jan 29, 2018

Copy link to clipboard

Copied

Sure, there are various ways. Here's one:

var s = "52.6531"

var indexOfPeriod = s.indexOf(".");

var decimalPart = indexOfPeriod==-1 ? "" : s.substr(indexOfPeriod+1);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 29, 2018 Jan 29, 2018

Copy link to clipboard

Copied

To copy the decimal value to a field add this to the end of the code above:

this.getField("Decimals").value = decimalPart;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 29, 2018 Jan 29, 2018

Copy link to clipboard

Copied

I used var s = this.getField("Text1").value; instead of "52.6531" because I do not know the exact value and I should get from the "text1" text field. Now, I am getting the error stating "s.indexOf is not function". If you want, I can explain How my output should looks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 29, 2018 Jan 29, 2018

Copy link to clipboard

Copied

Sometimes the values of "Text1" field will be 10,000.0000. So, I need to get the exact value.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 29, 2018 Jan 29, 2018

Copy link to clipboard

Copied

Use:

var s = this.getField("Text1").valueAsString;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 29, 2018 Jan 29, 2018

Copy link to clipboard

Copied

Apologies, now requirement has been entirely changed.

Now, I am going to keep 3 fields total (Text1, Text2, Text3).

In Text1 values will be only in numbers includes "thousand separators", "decimals" and "negative values" should be replaced as "parentheses".

In Text2 values will be currency type either they can type Dollar or $. So, that should be consider as text field.

In Text3 values should be "Amount of:" + Text2 and Tex1" (The output should be) for example: Amount of: Dollar 1,000.0000

if the text1 value is "1" and text2 has Euro example: Amount of: Euro 1 if the value is more than thousand we need to include thousand separator.

Value should come as exactly from Text1 including the number of decimals. if "text 1" has "4,556.56412334" or it might come without decimal also "4,556" or 4,556.20 or 4,556.00. So, output should be "Dollar 4,556.56412334" or "4,556" or 4,556.20 or 4,556.00 and If it is negative values we should be replace "-" to parentheses "-4,556.56412334" to (4,556.56412334)

Suppose if they add additional space at the end value from text2 "Dollar " it should replace as "Dollar".

Could you help me to complete this??

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 29, 2018 Jan 29, 2018

Copy link to clipboard

Copied

LATEST

I suggest you spend some time studying the core JS-syntax.

If you want me to write this code for you feel free to contact me privately (try6767 at gmail.com) and we could discuss it further, including the price.

Votes

Translate

Translate

Report

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