Decimals in output of the numeric value, use of parseInt | Community
Skip to main content
AdobeCampaignProxd
Level 2
February 19, 2025
Solved

Decimals in output of the numeric value, use of parseInt

  • February 19, 2025
  • 1 reply
  • 582 views

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.

Best answer by SushantTrimukheD

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

1 reply

SushantTrimukheD
SushantTrimukheDAccepted solution
Level 4
February 20, 2025

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

AdobeCampaignProxd
Level 2
February 21, 2025

Thank you very much. Works!