how to convert a number to a special formatted string

M

Maggie

I have an inforpath templated created from database, there's one field ID
which is a number in database, I want to display it as an 8 characters string
left padded with '0'. For example, id '1234' displayed as '00001234'. How can
I do that?

Thanks a lot.
 
F

Franck Dauché

Hi Maggie,

You could do it with code behind (scripting or Managed Code).

Regards,

Franck Dauché
 
M

Maggie

Thank you Franck. Could you give me some samples or links about how to do
code behind with infopath? I just have no clue where to start with.

Thanks,

Maggie
 
F

Franck Dauché

Hi Maggie,

If you have 2 textboxes on your form linked to 2 fields: field1 as decimal
and field2as text for a 3-digit display. Next you have a button with that
code:

var nodeSource = XDocument.DOM.selectSingleNode("//my:field1");
var m = nodeSource.text;
if (m < 10)
{m = "00" + m; }
else if (m < 100)
{m = "0" + m;}
var nodeTarget = XDocument.DOM.selectSingleNode("//my:field2");
nodeTarget.text = m;

This simple code will return: 008, if you enter 8 in the first checkbox.
023 for 23, etc. Just adapt this to your case.

Hope that it helps.

Regards,

Franck Dauché
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top