You are viewing limited content. For full access, please sign in.

Question

Question

Formatting number variables in custom HTML

asked on June 2

I'm using a number field as a variable which is written to an HTML field.  When looking at the field data I can see it is formatted with comma separators, but it loses the commas when shown in the HTML.  Is there any way to format the HTML to include comma separators?  I've tried with a number field as well as a currency field and they are both reacting the same.

 

Below is a simple example for visibility

1 0

Answer

SELECTED ANSWER
replied on June 3

As Matthew mentioned, custom JavaScript is needed for this at the moment. 

For your particular case, it can be done as follows. 

Create a sibling text field, e.g. 'FormattedCurrency', set as hidden. Then include the following JS

 

function formatNumberToText(numberFieldId, textFieldId) {
  LFForm.onFieldChange(() => {
    const raw = LFForm.getFieldValues({ fieldId: numberFieldId });
    const formatted =
      raw === null || raw === "" || isNaN(+raw)
        ? ""
        : Number(raw).toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 2 });
    LFForm.setFieldValues({ fieldId: textFieldId }, formatted);
  }, { fieldId: numberFieldId });
}

formatNumberToText(6,8);


Where 6 is is field id of the currency, and 8 is the field Id of the 'FormattedCurrency' single line field. The {/dataset/FormattedCurrency} can be used the rich text. 

The toLocaleString options can be modified based on your needs. 

2 0
replied on June 4

This worked great Joey

 

Thank you!

1 0

Replies

replied on June 3

I recently posted a similar item about the decimal value not being included on one.

https://answers.laserfiche.com/questions/237027/Including-Decimals-on-Variables-In-Custom-HTML-Fields#repliestomain

It would be a nice enhancement to improve options for low-code/no-code solutions.

Right now, JavaScript appears to be the only option to address this need.

2 0
replied on June 3

Are you using JavaScript to move the variable to a custom HTML or does it show after the initial Amount field has been submitted?

0 0
replied on June 3

Hi Angela

 

There isn't any JavaScript.  I'm just using the field value variables in the HTML

 

 

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.