Decimal separator in currency field (not a comma)

Forums General discussion Decimal separator in currency field (not a comma)

This topic contains 7 replies, has 2 voices, and was last updated by  Ayanda Makhathini 4 days, 10 hours ago.

  • Author
    Posts
  • #22957

    Ayanda Makhathini
    Participant

    Hi there

    I have a form which does simple calculations but the issue I’m having is with the currency field not having comma’s instead of fullstops(dots). I’m using a CCDD (Cascading drop down) to select the product which selects the price field from the lookup list which is of type currency but returns it in the current list as single line ,I then have a h_price field that is of type currency which is set with a dot in it . There are numbers in the fields but the form wont save unless i change dots to commas. I’m also using (calc:) in rules to set the correct values, Can someone point me in the right direction for the solution of this

    Attachments:
  • #22960

    Alexander Bautz
    Keymaster

    The problem is that the field value pulled from the db by the query is returned as a number with dot as decimal separator. If you need to use this number in a currency or number fields and your locale number format expects a comma you must replace the dot with a comma. How are you setting the value in the hidden fields?

    Alexander

  • #22965

    Ayanda Makhathini
    Participant

    hi Alex

    I’m using the the CCDD to select the product from another list which selects the price in the lookup list which is a currency field. I’m using a DFFS rule in the current list to set the h_price field which is a currency field. I’m trying to use a Custom JS function to change decimals to comma’s when the field changes but my syntax is not that great

  • #22968

    Ayanda Makhathini
    Participant

    Hi Alex

    I also noticed that my calculations are not rounding off to 2 decimal places. I have the value 6118.43 pulling in as 6118.425 on the form. When I calculate (value) x (quantity) = (6118.425 x 2 = 12236850) . After that I calculate VAT and Total excluding VAT (VAT=15%). Amount including VAT is 14072.37 but i get 14072377.5 which will save as 14 Million if i replace comma. Would adding a custom function to round off solve the issue or is there a shorter way around it. I managed to remove decimals and replace them with commas but this wont if I’m rounding off to wrong decimal place. I currently have my calculations set in the DFFS rules. Amount exluding VAT = calc:{h_price}*{Quantity} , Total including VAT – calc:{Amount}*0.15+{Amount} . I think my main issue is to round off now

    regards

    Attachments:
  • #22973

    Alexander Bautz
    Keymaster

    You have better control is you use a custom function to do the calculation. Here is an example you can call from a rule (in the Run these functions field):

    function doMyCalculation(){
        var a = getFieldValue("FieldInternalName1");
        var b = getFieldValue("FieldInternalName2");
        // Do the calculation
        var c = a * b;
        // Truncate to two decimals
        var d = c.toFixed(2);
        // Replace . with ,
        var e = String(d).replace(".",",");
        // Write to another field
        setFieldValue("TargetFieldInternalName",e);
    }

    Alexander

  • #22991

    Ayanda Makhathini
    Participant

    Hi Alex

    Thank you for the function it works.The calculation,rounding off and replace comma works well but i noticed that I’m losing couple cents if I dont round off the value of the item which isn’t rounded off when i get it from look up list, I’m using the function below to get the value(hprice) and try using the toFixed(2) but i get a error. Not sure yet but also working on it.

    function convertprice()
    {
    var hPrice = getFieldValue(“h_price”);

    var priceValueFixed = hPrice.toFixed(2);

    setFieldValue(“h_price”,priceValueFixed);

    }

    Regards

    Attachments:
  • #22996

    Alexander Bautz
    Keymaster

    The function toFixed only works with numbers and the value you get from getFieldValue is string and not number. You can convert it like this:

    function convertprice(){
      var hPrice = getFieldValue("h_price");
      if(hPrice !== ""){
        var priceValueFixed = Number(hPrice).toFixed(2);
        setFieldValue("h_price",priceValueFixed);
      }
    }

    You might want to change the toFixed method with Math.round like this:

    var priceValueFixed = Math.round(Number(hPrice) * 100) / 100;

    Alexander

  • #23112

    Ayanda Makhathini
    Participant

    Hi Alex

    I used var price = parseFloat(getFieldValue(“Price”)).toFixed(2); to convert to number then i did calculations then converted to string with comma, You helped me alot. Thank you very much

    Regards
    Mak

You must be logged in to reply to this topic.