Skip to main content
shobhits6437
August 8, 2023
Question

299.65 is automatically converting to 299.64.

  • August 8, 2023
  • 22 replies
  • 0 views

Hi,

Whenever I'm trying to enter 271.64 or 2**.65 where (** can be any number) it's getting converted to 2**.64

I'm using below expression for this:

a!floatingPointField(
label: "Test Decimal Value",
value: todecimal(fixed(trunc(ri!value, 2))),
saveInto: ri!value
)

Not sure, if this is an issue from Appian. Can anyone confirm this.

Thanks in advance.

    22 replies

    August 8, 2023

    is it necessary to truncate upto 2 decimals?once try with 3 decimals.

    shobhits6437
    August 8, 2023

    It's working with three decimal values, but business requirement is to keep up to two decimal values. And this is happening only in this scenario not with any other numbers. Like:

    August 8, 2023

    have you tried with trim()?

    stefanhelzle0001
    Brainy
    August 8, 2023

    There is nothing "converted". This is just the way how the values is displayed in both places.

    shobhits6437
    August 8, 2023

    Hi Stefan, Thanks for your reply. But, issue is it's only happening with this specific number 2**.65 which is converting into 2**.64.

    It's working with three decimal values, but business requirement is to keep up to two decimal values. And this is happening only in this scenario not with any other numbers. Like:

    stefanhelzle0001
    Brainy
    August 8, 2023

    There is a general problem with how computers store decimal values. Check this:

    https://www.baeldung.com/cs/floating-point-numbers-inaccuracy

    You will see this with a lot of numbers. Depending in the internal representation it works most of the time and sometimes not. And there is no real fool proof way of managing this. Mathematical rounding should get you covered most of the time. This conversion to a string and back to a decimal will probably not.

    August 8, 2023

    Hi, Please try to use this code which will give you 299.65

    a!floatingPointField(
      label: "Test Decimal Value",
      value: todecimal(fixed(trunc(299.65, 3))),
    )

    shobhits6437
    August 8, 2023

    Hi Harshitha,

    Thanks for the solution, but here we are passing both parameters of trunc function as rule inputs:

    August 8, 2023

    Can you please try this code which will solve your issue

    a!floatingPointField(
    label: "Test Decimal Value",
    value: todecimal(fixed(299.65,2))
    )

    jamesm4933
    August 8, 2023

    Fascinating bug you found there.  

    From my testing, the issue seems to be with the trunc() function, fixed() does not exhibit the same behavior. 

    I recommend reducing your expression to: fixed(ri!value, 2)

    It is not necessary to use todecimal(), as the a!floatingPointField() performs the cast as well.

    So, right now, you are truncating as a floating point number, converting to a string, and setting a fixed number of characters, then converting BACK to a floating point number, for a field than then casts to a floating point number automatically.  A few more steps than necessary, I should think.

    Take care,

    JM

    csteward
    August 8, 2023

    I agree with fixed() only, and I would place the 'conversion' on the saveInto vs the display, try:

    EDIT:  Code updated

    a!floatingPointField(
      label: "Test",
      value: ri!value,
      saveInto: a!save(
        ri!value,
        if(
          rule!APN_isEmpty(save!value),
          0.00,
          todecimal(fixed(save!value,2))
        )
      )
    )

    stefanhelzle0001
    Brainy
    August 8, 2023

    You are aware that fixed() returns a string and you are back into an auto type conversion cycle?

    August 9, 2023

    Try to use fixed(ri!value,2) instead of todecimal(fixed(trunc(ri!value, 2))) under value config.

    shobhits6437
    August 9, 2023

    Already tried, but this rounds off to next integer value, which is not the expectation here. Like:

    csteward
    August 9, 2023

    If we don't want rounding, the combination of only fixed() and trunc() should do this for you:

    a!floatingPointField(
      label: "Test",
      value: ri!value,
      saveInto: a!save(
        ri!value,
        if(
          rule!APN_isEmpty(save!value),
          0.00,
          fixed(trunc(save!value,2),2)
        )
      )
    )