Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

Numeric Formatting in JavaScript

Avatar

Level 3

I'm using the following in a validation event

var MBn = (iSizeN / 1383.27)/ 1024;

The result is a digit with around 10 decimal places.

1) What do I need to do so that the result is only 2 decimal places?

2) How do I then convert the 2-decimal place number to a text string so that I can use it in a message? Note that I still need access to the MBn variable so the string variable would be MBs.

Thank you for the help.

1 Accepted Solution

Avatar

Correct answer by
Level 4

2. is easy, you don't need to formate it...


Textfield1.rawValue = "Text1 " + Math.round(MBn*100) /100 + " Text2";

For the first question:

To say the truth I am not sure if I got your problem.

MBn = Math.round(MBn *100) /100

MBn has two after digits then.

Only the "event" you put it in feels somehow... strange to me.

View solution in original post

6 Replies

Avatar

Former Community Member

Probably one of the easiest ways to do this is to convert the number to a string and look for the decimal place in the string.  Alternatively there are a number of Math.xx() functions that you could use.

The attached sample shows the integer and decimal values of the number entered by the user.

For general rounding you could use "num.toFixed(3);" or "num.toPrecision(4);" to get either a fixed number of decimals or a precise number of numbers in your field.

Avatar

Correct answer by
Level 4

2. is easy, you don't need to formate it...


Textfield1.rawValue = "Text1 " + Math.round(MBn*100) /100 + " Text2";

For the first question:

To say the truth I am not sure if I got your problem.

MBn = Math.round(MBn *100) /100

MBn has two after digits then.

Only the "event" you put it in feels somehow... strange to me.

Avatar

Level 3

Thank you ocent12 -- your suggestion worked.

I though I can to convert the number value to a string, before I could use it in text...but that is not the case.

To understand what I'm doing, I have an ImageField and the user can add a JPG file. I'm then calculating the size of the JPEG to make sure that it's under 1MB. The calculation is a bit wonky, but it works! :-)

Thanks again.

Avatar

Level 10

Hi David,

Because we are at the start here, it may be better to the intended type of variable at the start of the name; e.g. nMB (with the "n" at the start to signify number). Then "s" for string sMB. Personal preference...

So in FormCalc:

var nMB = (iSizeN.rawValue / 1383.27) / 1024

var sMB = Round(nMB, 2)

var sNote = Concat("MB is equal to ", sMB, ".")

xfa.host.messageBox(sNote, "Message...", 3 , 1)

That will display a message box and you will still have the variable sMB (to 2 decimal places) available to you for further scripting.

Good luck,

Niall

PS - Already sorted...

Avatar

Level 3

It's a good idea to start setting a coding standard for this stuff. Good suggestion.

Perhaps I should start another thread for this, but....

I have to use the following function 12 times in my form.

var iImage = xfa.resolveNode("NA-Image-1");

var nSize = iImage.value.image.value.length ;

var nMB = nSize / 1383.27)/ 1024;

if (nMB > 1.2)

{

    this.validationMessage = "This image is larger than 1 Megabyte. It is " + nMB + ". Please resize your image. "; 

    0;

}

The only thing that will change is the name of the field in line one of the code. Could I create a function and then simply call that function with the specific field name that i need?

Getting me started on this is appreciated.

Avatar

Level 3

I figured this out...and it's pretty easy too.