Code for Calculating Square Root - InfoPath Dev
in

InfoPath Dev

Code for Calculating Square Root

Last post 08-22-2012 07:46 AM by Jimmy. 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 06-20-2006 10:26 AM

    Code for Calculating Square Root

    I have several fields on my form which require, as part of the calculation, to calculate the square root of another field. Since square root isn't one of the basic functions shown in the insert function window, I assume I'm going to need to write some code to do this. Does anyone know how I would go about this? Thanks.
    Sandy
  • 06-21-2006 12:12 AM In reply to

    Re: Code for Calculating Square Root

    Hi
    Check out this link. I hope this will help.

    http://msdn2.microsoft.com/en-us/library/system.math.sqrt.aspx

    Madhuri
    http://www.ggktech.com
  • 06-21-2006 05:08 AM In reply to

    Re: Code for Calculating Square Root

    That's exactly what I need. Thanks!
  • 06-22-2006 06:54 AM In reply to

    Re: Code for Calculating Square Root

    Okay, one problem I forgot to mention. If I were having the user fill out this form in Infopath, I'd add a button and put the square root code behind it. However, my company has developed an app which the customer uses to populate my Infopath form. Instead of the app calculating the square root of various fields, then putting the values on my form, I'd like to have Infopath do all the calculations. Is there any way to use this code to have Infopath calculate the square root of fields on my form? Thanks.
  • 07-06-2006 05:36 AM In reply to

    • skaaal
    • Not Ranked
    • Joined on 05-23-2006
    • Italy
    • Posts 7

    Re: Code for Calculating Square Root

    XdMath has no sqrt support but Javascript does, so I use the following workaround:
    - create a new field called "sqrt<originalfield>" as double.
    - on the validation rules for the original field, click to add an OnAfterChange event
    - add the final part of the code below to your script ; this JS code will populate the sqrt field with the square root value.
    - use the sqrt<originalfield> in your computations.

    I hope it helps,
    Pascal.

    ------------

    function msoxd_my_PortataVentilatore::OnAfterChange(eventObj)
    {
    // Codice per il ripristino dello stato globale.

    if (eventObj.IsUndoRedo)
    {
    // È stata eseguita un'operazione Annulla o Ripeti e il modello DOM è in sola lettura.
    return;
    }

    // Un campo è stato modificato e il modello DOM è in scrittura. Inserire il codice di risposta per le modifiche.
    var varNodo = XDocument.DOM.selectSingleNode("//my:PortataVentilatore").text;
    if (varNodo != "")
    {
    XDocument.DOM.selectSingleNode("//my:SqrtPortataVentilatore").text = Math.round(Math.sqrt(varNodo));
    }
    else
    {
    XDocument.DOM.selectSingleNode("//my:SqrtPortataVentilatore").text = "";
    }
    }
  • 07-06-2006 06:07 AM In reply to

    Re: Code for Calculating Square Root

    This looks great, but can anyone translate this to English for me? Thanks.
  • 07-07-2006 09:08 AM In reply to

    • skaaal
    • Not Ranked
    • Joined on 05-23-2006
    • Italy
    • Posts 7

    Re: Code for Calculating Square Root

    Here it is:

    function msoxd_my_<yourFieldName>::OnAfterChange(eventObj)
    {
    // some Infopath-generated comment

    if (eventObj.IsUndoRedo)
    {
    // some Infopath-generated comment
    return;
    }

    // some Infopath-generated comment

    // **** CODE STARTS HERE ****
    var varNodo = XDocument.DOM.selectSingleNode("//my:<yourfieldname>").text;
    if (varNodo != "")
    {
    XDocument.DOM.selectSingleNode("//my:Sqrt<yourfieldname>").text = Math.sqrt(varNodo);
    }
    else
    {
    XDocument.DOM.selectSingleNode("//my:Sqrt<yourfieldname>").text = "";
    }
    // **** CODE ENDS HERE ****
    }

    _________________
  • 07-07-2006 11:24 AM In reply to

    Re: Code for Calculating Square Root

    Thank you so much!!!
  • 08-22-2012 05:18 AM In reply to

    • Fidzl
    • Not Ranked
    • Joined on 08-22-2012
    • Posts 1

    Re: Code for Calculating Square Root

    Hello there,

     i still have a problem with this sqrt-stuff.

    i used the posted code and it works, but not totally how i needed ist. with this code, both fields have the data-type text(string). i would like to keep on working with the squareroots.

    For example Field1 = sqrt of x and Field2 = Field1 * 3

     Is this possible to realize?

    Thank you very much!

     

  • 08-22-2012 07:46 AM In reply to

    Re: Code for Calculating Square Root

    I don't generally advocate using code when you don't understand how it works, but this thread has a completely codeless square root implementation.  Once it finishes, you can copy the Result value to Field1, and Result * 3 to Field2.

    http://www.infopathdev.com/forums/p/21041/72904.aspx#72904

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
Page 1 of 1 (10 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.