calc in set field error

Forums General discussion calc in set field error

Viewing 4 reply threads
  • Author
    Posts
    • #32308
      Jeff Lynch
      Participant

      I’m having the strangest thing, when I use calc:{currencyfield1}+{currencyfield2} in the set field value for a New form, it works great – 10000 + 10000 = 20000, no problem, then on edit it returns 20.00. I figured out why but I’m not sure how to solve it. basically its seeing the comma (,) inserted when the number field is saved on the edit and interpreting it as a decimal point.

      To test my theory I manually removed the commas on the edit screen and it works great.

      Any idea how to address this issue?

    • #32310
      Alexander Bautz
      Keymaster

      Hi,
      It looks like it is struggling with the number format – can you show me how the numbers are shown in EditForm – when it evaluates to 20?

      Alexander

    • #32312
      Jeff Lynch
      Participant

      it’s inserting the commas at the thousand spot, then interprets those as decimals. As a temporary fix I created a calculated field and added them all together.

      As a test I removed the commas in the edit form and when I did that it calculates the sum correctly.

    • #32314
      Alexander Bautz
      Keymaster

      The problem was related to identifying what is thousand separator and what is decimal separator (these are not the same in all languages). The function I used to convert the string (from the input field) to a number did not work properly if the number had a thousand separator but not a decimal separator – it misinterpreted the thousand separator as decimal separator.

      I’ll address this in the next release, but if you could try dropping the below snippet in your Custom JS and see if it works correctly it would be great.

      Best regards,
      Alexander

      spjs.dffs.getNumberFormat = function(){
          if(spjs.dffs.data.numberFormat === undefined){
              if(spjs.dffs.data.isSP07 || spjs.dffs.data.isSP10){
                  spjs.modal.add({
                      "title": "You must specify the number format",
                      "html": "To use the function strToNum in SP 2007 or SP 2010 you must add this to your Custom JS:<pre>spjs.dffs.data.numberFormat = {\n  \"ThousandSeparator\": \",\",\n  \"DecimalSeparator\": \".\"\n}</pre>Change the settings to match your locale number format like it shows in the number fields in your form."
                  });
              }else{
                  jQspjs.ajax({
                      "url": _spPageContextInfo.webServerRelativeUrl + "/_api/Web/RegionalSettings",
                      "type": "GET",
                      "async": false,
                      "headers": {
                          "Accept": "application/json;odata=verbose",
                          "content-type": "application/json;odata=verbose"
                      },
                      "success": function(data){
                          spjs.dffs.data.numberFormat = data.d;
                      },
                      "error": function(err){
                          // console.log(err);
                      }
                  });
              }
          }
          return;
      };
      
      spjs.dffs.strToNum = function (str) {
          if (typeof str === "number") {
              return str;
          }
          if (str === "" || typeof str !== "string") {
              return 0;
          }
          if(spjs.dffs.data.numberFormat === undefined){
              spjs.dffs.getNumberFormat();
          }
          str = str.split(spjs.dffs.data.numberFormat.ThousandSeparator).join("");
          str = str.split(spjs.dffs.data.numberFormat.DecimalSeparator).join(".");
          var n = parseFloat(str);
          return n;
      };
    • #32322
      Jeff Lynch
      Participant

      this code worked!!! Thank you!

Viewing 4 reply threads
  • You must be logged in to reply to this topic.