Jump to content

Can I place a character in a FormatNumber function?


DSweet

Recommended Posts

I have a project where I am going to have a barcode printed on a card and the human readable number is to print underneath the barcode. However, the human readable number needs to be dashed. The number string is always 16 characters long and there needs to be a dash separating every 4th digit (i.e. 0000-0000-0000-0000). I know that I can split up the number using either a bunch of Mid(str, start, length) or "splice" functions for every section, but I wondered if there was a way of doing this using a FormatNumber function. I tried the usual settings for FormatNumber that I thought of but nothing worked.

 

FormatNumber("0000-0000-0000-0000", Field("hrBarcode"));

FormatNumber("XXXX-XXXX-XXXX-XXXX", Field("hrBarcode"));

FormatNumber("####-####-####-####", Field("hrBarcode"));

 

I even tried putting a comma there and then I would replace it out again

 

FormatNumber("0000,0000,0000,0000", Field("hrBarcode"));

 

However nothing worked. The only one to actually change the number was the comma format, but it always put the comma in a numeric fashion (every three digits).

 

Note: Oddly, even when I used a very limited formula of just FormatNumber("0,", Field("hrBarcode")) it still put the entire number in comma formation - 0,000,000,000,000,000. So if people want to use the comma notation, all you really need to put in the formula is one number marker and one comma.

 

Back to my post question. Is there some format string that I can use in the FormatNumber() function that will place a dash, or any other specified character, every 4th character in my number string?

Link to comment
Share on other sites

My first idea would be to add the dashes in the data since it would be much easier to remove them with JS for the actual barcode than to add them to the human readable version.

 

With that said, I attempted to find a one line solution to your problem. It showcases my current ability with JS (which I'm sure Dan can reduce to half the number of characters). I welcome his revision so that I can learn, but I'm proud of this (lengthy) one-liner:

 

return Left(Str(Field("MY_BARCODE")).replace(/\d{4}/g,"$&-"),Len(Str(Field("MY_BARCODE")).replace(/\d{4}/g,"$&-"))-1);

Link to comment
Share on other sites

Here's another option assuming your barcode numbers are always the same length:

(my example uses a 12-digit number)

 

str = Field("POSTNET Barc").substr(0,4) + "-" + Field("POSTNET Barc").substr(4,4) + "-" + Field("POSTNET Barc").substr(8,4);
return str;

Link to comment
Share on other sites

Here's my last one (I know, I'm bypassing the original question regarding whether it can be done with the FormatNumber syntax, but I have learned a lot about JS this morning!):

 

function insertNthChar(string,chr,nth) {
 var output = '';
 for (var i=0; i<string.length; i++) {
   if (i>0 && i%nth == 0)
     output += chr;
   output += string.charAt(i);
 }

 return output;
}

return insertNthChar(Field("MY_BARCODE"), '-', 4);

Link to comment
Share on other sites

Thanks Eric,

 

All of your solutions work fine and gives me other options. However, I would still like to know if there is a method using the original FormatNumber function.

 

Or maybe that rule should be renamed FormatCertainNumbers? :rolleyes:

Link to comment
Share on other sites

I think we have both exhausted the FormatNumber options. I can't find a solution using that method. I notice that FormatNumber is not a standard JavaScript method which leads me to believe that it is a custom method created by Printable without the foresight of other uses like you have now.

 

I had thought there was a thread here somewhere with all the possible options for the use of FormatNumber, but I can't find it if did exist.

Link to comment
Share on other sites

My first idea would be to add the dashes in the data since it would be much easier to remove them with JS for the actual barcode than to add them to the human readable version.

 

With that said, I attempted to find a one line solution to your problem. It showcases my current ability with JS (which I'm sure Dan can reduce to half the number of characters). I welcome his revision so that I can learn, but I'm proud of this (lengthy) one-liner:

 

return Left(Str(Field("MY_BARCODE")).replace(/\d{4}/g,"$&-"),Len(Str(Field("MY_BARCODE")).replace(/\d{4}/g,"$&-"))-1);

Thanks to a good friend who now works for Google, I can shorten my original 1-liner to this:

return Str(Field("MY_BARCODE").replace(/(\d{4})(?!$)/g,"$&-"));

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...