Numeric Formatting in JavaScript | Community
Skip to main content
DrDave6
Level 3
April 21, 2009
Solved

Numeric Formatting in JavaScript

  • April 21, 2009
  • 6 replies
  • 4870 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by ocen12

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.

6 replies

April 21, 2009

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.

ocen12Accepted solution
Level 4
April 21, 2009

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.

DrDave6
DrDave6Author
Level 3
April 21, 2009

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.

Niall_O_Donovan
Level 10
April 21, 2009

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

DrDave6
DrDave6Author
Level 3
April 21, 2009

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.

DrDave6
DrDave6Author
Level 3
April 21, 2009

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