Expand my Community achievements bar.

SOLVED

Decimals in output of the numeric value, use of parseInt

Avatar

Level 2

Hello, 

 

Im looking for the next numeric format: I get the value(targetData.val_generic_2) 50000,98 from the DB

 

(keep in mind that the dataType (type="") os the variable in our DataScheme is "Double")

 

In my include I have this: 

 

<%= parseInt(targetData.val_generic_2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".") %>

 

With this parseInt I manage to transform 50000 into 50.000... It's way more estethic. But I need 50.000,98... the comma and the last 2 numeric values.

 

There's a simple way to do this? I can enhance the parseInt with more parameters or I should do JS inside the include personalization block.

 

Thank you.

1 Accepted Solution

Avatar

Correct answer by
Level 3

Hi @AdobeCampaignProxd ,

Try this,


<%= targetData.val_generic_2.toFixed(2).replace('.', ',').replace(/\B(?=(\d{3})+(?!\d))/g, ".") %>

Breakdown:

  1. toFixed(2) ensures that the value has exactly two decimal places. If there are fewer than two decimals in your value, it will pad it with zeroes (e.g., 50000.98 becomes 50000.98).
  2. .replace('.', ',') changes the decimal point to a comma (e.g., 50000.98 becomes 50000,98).
  3. .replace(/\B(?=(\d{3})+(?!\d))/g, ".") adds the thousands separator (e.g., 50000 becomes 50.000).

This should give you the result 50.000,98 as you are expecting.

Thanks

View solution in original post

2 Replies

Avatar

Correct answer by
Level 3

Hi @AdobeCampaignProxd ,

Try this,


<%= targetData.val_generic_2.toFixed(2).replace('.', ',').replace(/\B(?=(\d{3})+(?!\d))/g, ".") %>

Breakdown:

  1. toFixed(2) ensures that the value has exactly two decimal places. If there are fewer than two decimals in your value, it will pad it with zeroes (e.g., 50000.98 becomes 50000.98).
  2. .replace('.', ',') changes the decimal point to a comma (e.g., 50000.98 becomes 50000,98).
  3. .replace(/\B(?=(\d{3})+(?!\d))/g, ".") adds the thousands separator (e.g., 50000 becomes 50.000).

This should give you the result 50.000,98 as you are expecting.

Thanks

Thank you very much. Works!