Skip to main content
Participant
August 20, 2020
Answered

Combining text and number fields without loosing number field format.

  • August 20, 2020
  • 2 replies
  • 1360 views

In my job function I have to combine fields to generate letters for different departments. I can usually cobble together a working form, but in this case I'm hitting a wall. I'm trying to combine a text field with a number field without loosing my number field formatting.

 

Example: The following results in "The cost is 1000" while I am trying to get "The cost is $1,000.00"

var cost = "The cost is ";
var OS = this.getField("Old Salary").value

event.value = cost + OS;

 

 

This topic has been closed for replies.
Correct answer Nesa Nurani

Just move comma from this $9,9999 to this $99,999 but be warned then it won't show 1,000 it will show 10,00

 

EDIT: If you want to show both 1,000 and 10,000 replace this line: v = util.printx("$9,9999", v);

with this: v = util.printf("$%,0.0f", v);

If you also want to add decimal,replace second zero with number of desired decimals.

 

2 replies

Jason5ECCAuthor
Participant
August 21, 2020

This worked exactly as I wanted. Thanks!

 

var v = this.getField("Old Salary").value;
v = util.printf("$%,0.2f", v);
event.value = "The cost is " + v;

ls_rbls
Community Expert
Community Expert
August 22, 2020

Hey Jason!

 

Glad that this helped. Don't forget to mark NesaNurani's answer as correct solution, thank you.

ls_rbls
Community Expert
Community Expert
August 20, 2020

If this is just to display an output (not to be calculated any further with other fields), then You can use custom calculation script with arbitraty mask using the "util.printx()" method

 

Use it like this :

 

var v = this.getField("Old Salary").value;
v = util.printx("$9,9999", v);
event.value = "The cost is " +v;
Jason5ECCAuthor
Participant
August 21, 2020

This is great! Is there a way to format it if the number of digits isn't fixed? In the above example the "$10,000" comes out as "$1,0000".

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
August 21, 2020

Just move comma from this $9,9999 to this $99,999 but be warned then it won't show 1,000 it will show 10,00

 

EDIT: If you want to show both 1,000 and 10,000 replace this line: v = util.printx("$9,9999", v);

with this: v = util.printf("$%,0.0f", v);

If you also want to add decimal,replace second zero with number of desired decimals.