Need to restrict upto 2 decimal places

Certified Associate Developer

Hi All,

i have floatingpoint field in my UI, i want to restrict user from typing values upto 2 decimal point and Below is my expected results

  • If the user enters 12, it will be rounded off to 12.00
  • If the user enters 12.1, it will be rounded off to 12.10
  • If the user tries to enter 12.678, it will not be allowed. The entry will be cut off at 12.67

but what is the issue am facing is its allowing me to enter more values after decimal point and also if i give round values it's not taking .00 value.

below is my code

a!floatingPointField(
  label: "Dental Premium (Annual)",
  labelPosition: "ABOVE",
  value: ri!intakePreminumDetails['recordType!{4d82eb7f-ba46-46a2-a7dc-969f4ffcf344}GSE_SCL Intake Premium Details.fields.{cef89c65-a133-4766-bbb5-93cbcceddcab}dentalPremium'],
  saveInto: {
    a!save(ri!intakePreminumDetails['recordType!{4d82eb7f-ba46-46a2-a7dc-969f4ffcf344}GSE_SCL Intake Premium Details.fields.{cef89c65-a133-4766-bbb5-93cbcceddcab}dentalPremium'],
    if(a!isNotNullOrEmpty(save!value),fixed(save!value,2),null)),
   
  },
 

my rule input source is recordtype. and which of data type as decimal and also in db its decimal(10,2) .
note(am using external db (sql server management studio) using connected system)

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    Easiest to just use a Text Field and sanitize the input similarly to how you already are.  You won't be able to prevent the user from typing some gibberish into the field, but you can at least instantly justify it.

  • 0
    Certified Lead Developer
    in reply to Mike Schmitt

    Example provided: we're using "fixed" for the display-time to make sure all decimal places are DISPLAYED even when they are zero.

    (note: you do not need to STORE these extra places in the "decimal" data, as it is actually impossible to do so - if it should be SHOWN to the end user, it's up to you to do that on the user-facing side.) 

    "Save into" is null-checked and truncated to 2 decimal places upon entry.  As I mentioned originally, of course, nothing can really keep the user from entering extra spam digits after the decimal place, but they are not saved.  I don't know of any good workaround for that unfortunately - if you were dedicated to it, you could look into using the "character count" feature, but I don't know if you'd be able to update it dynamically enough to account for different values ahead of the decimal point.

    a!localVariables(
      local!testDecimal: null(),
    
      a!textField(
        label: "Fake decimal entry",
        refreshAfter: "KEYPRESS",
        placeholder: "Enter Decimal Value",
    
        value: if(
          a!isNullOrEmpty(local!testDecimal),
          null(),
          fixed(local!testDecimal, 2)
        ),
        saveInto: {
          a!save(
            local!testDecimal,
            if(
              a!isNotNullOrEmpty(save!value),
              todecimal(trunc(todecimal(save!value), 2)),
              todecimal(null())
            )
          )
        }
      )
    )

Reply
  • 0
    Certified Lead Developer
    in reply to Mike Schmitt

    Example provided: we're using "fixed" for the display-time to make sure all decimal places are DISPLAYED even when they are zero.

    (note: you do not need to STORE these extra places in the "decimal" data, as it is actually impossible to do so - if it should be SHOWN to the end user, it's up to you to do that on the user-facing side.) 

    "Save into" is null-checked and truncated to 2 decimal places upon entry.  As I mentioned originally, of course, nothing can really keep the user from entering extra spam digits after the decimal place, but they are not saved.  I don't know of any good workaround for that unfortunately - if you were dedicated to it, you could look into using the "character count" feature, but I don't know if you'd be able to update it dynamically enough to account for different values ahead of the decimal point.

    a!localVariables(
      local!testDecimal: null(),
    
      a!textField(
        label: "Fake decimal entry",
        refreshAfter: "KEYPRESS",
        placeholder: "Enter Decimal Value",
    
        value: if(
          a!isNullOrEmpty(local!testDecimal),
          null(),
          fixed(local!testDecimal, 2)
        ),
        saveInto: {
          a!save(
            local!testDecimal,
            if(
              a!isNotNullOrEmpty(save!value),
              todecimal(trunc(todecimal(save!value), 2)),
              todecimal(null())
            )
          )
        }
      )
    )

Children
No Data