What is the most direct way to reproduce this printf-like format using ColdFusion?

I was cast in ColdFusion for a very simple job. The application has some logic for displaying "help codes" (even if it is not included in the help code), however, the logic is erroneous and needs to be fixed. Given a two-letter code, a 1-4 digit number and another 1-2 digits, I will need to display them like this call printf:

printf("%s%04d%02d", letterCode, bigNumber, smallNumber);

If you are not familiar with the printf function, it takes a format string (the first parameter) and writes other variables to it in accordance with this format. %smeans write a line, and %dmeans write a number; %0zdmeans "write the number and type it with zeros so that it has at least z characters (so it %04dmeans" write the number and type it with zeros so that it takes at least 4 digits ").

Here are some examples from %s%04d%02d:

"AD", 45, 12:  AD004512
"GI", 5121, 1: GI512101
"FO", 1, 0:    FO000100

However, this is my first time with ColdFusion, and I could not find anything like this printfeither sprintffor formatting strings.

, , () , , , .

+3
4
<cfset bigNumberPadded = NumberFormat(bigNumber,"0000")>
<cfset smallNumberPadded = NumberFormat(smallNumber,"00")>
<cfoutput>#letterCode##bigNumberPadded##smallNumberPadded#<cfoutput>

, , bpanulla, Leigh

<cfset args = ["AD", javacast("int", 45), javacast("int", 12)]>
<cfset output= createObject("java","java.lang.String").format("%s%04d%02d", args) >
+7

NumberFormat CF.

<cfoutput>#letterCode##NumberFormat(bigNumber, '0000')##NumberFormat(smallNumber, '00')#</cfoutput>
+4

Java, ColdFusion. Java:

http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html

Java, CFOBJECT CreateObject.

+1
source

I assume you display them on a web page? If so, I would use the switch / case statement. Since you said "with a two-letter code ...", the switch / chassis will work well. For instance:

<cfswitch expression="#twoLetterCode#">
    <cfcase value="aa12348">%s%04d%02d</cfcase>
    <cfcase value="bb23456">%s%05f%01e</cfcase>
    <cfcase value="cc97641">%s%08g%10j</cfcase>
    <cfdefaultcase>%s%04d%02d</cfdefaultcase>
</cfswitch>

Or you can use if / else instead. But the main question (to answer your question) is that in ColdFusion you just enter the display characters (be it help codes, text, or something else). You do not need to use a special function to display text on the page.

-1
source

Source: https://habr.com/ru/post/1794765/


All Articles