Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Issue rounding numbers in a textfield

Avatar

Level 4

Hi,  

I have text fields that are populated from XML data.  The data can be text or numeric, so I have a script in the initialized event to display a pattern if the data is a number:

this.format.picture.value = "num{zz9.9999%}";

Some of the numbers do not round correctly and this is causing manual editing which is a lot of work as I have to proof 250+ fields.  The point of automating these fields is to save time and effort.  Is there a solution to my problem?  I have found a few posts, but those solutions are not working for my situation - or at least I can't get it to work.

An example is a value of 0.0046875 and it should display as 0.4688%.  Instead it is displaying as 0.4687%.

1 Accepted Solution

Avatar

Correct answer by
Level 4

OMG - thank you soooo much - it works!

View solution in original post

5 Replies

Avatar

Employee

Hi,

your problem is that xx.yyyy5 is rounded down, xx.yyyy50001 is rounded up.

 

Try this approach for display in your initialize:

 

var content = parseFloat(this.rawValue);
if (!isNaN(content)) {
   this.format.picture.value = "num{zz9.9999%}";
   this.rawValue = content+0.000000001
}

 

0.0049995 will be rounded to 0.5000% as would 0.0049996

Avatar

Level 4

Thanks for your response, but this isn't working.  It still doesn't round to 0.4688% and I tried your example 0.0049995 and it displays 0.4999.

Avatar

Level 10

You're facing a typical rounding issue of the IEEE 754 standard. You can bypass this by converting the dicimals into integers before rounding.

 

Keep in mind: The display pattern plays an important role when you're going to display percentage values. 

So with the pattern num{zz9.9999%} the input have to be a value between 1 (= 100 %) and 0 (= 0 %). So a value of 0.0046875 = 0.4687 %

With a pattern num{zz9.9999 '%'} where the percentage symbol is quoted, the values are displayed this way. 0.0046875 = 0.0047 %

 

To get rid of the rounding issue multiply the input value by a factor that will convert all dicimal placed into an integer. Then round that integer and divide it by the factor again to recreate the initial decimal places. 

$ = Round(NumField1 * 1000000) / 1000000

 

 

 

Avatar

Correct answer by
Level 4

OMG - thank you soooo much - it works!