Number format: Left pad and remove decimal

I have a dollar amount: 128.56

I need to make it into a 6 byte field, left padded with zeros and the decimal removed (decimal implied) for a delimited flat file.

Taget format:  012856
Brian CoughterOwnerAsked:
Who is Participating?
Dain_AndersonConnect With a Mentor Commented:
Well, since we're using a multiplier (*100) to remove the decimal point in the function, the number can't be NULL; however, you could use a conditional IIF() with the delayed evaluation function DE() to accomplish it (there are other ways, but this is a pretty simple approach):

#iif(MyNumber GT 0, 'NumberFormat(MyNumber*100, 000000)', DE('000000'))#

<CFSET MyNumber = "128.56">
<CFOUTPUT>#NumberFormat(MyNumber*100, 000000)#</CFOUTPUT>


Brian CoughterOwnerAuthor Commented:
Great but one question...

I was using this:
#NumberFormat(MyNumber, 9999.99)#

If a value (MyNumber) was null, it would get 0.00 automatically.  How can I write your solution so that remains true?

Otherwise, I get an error on null values saying: "" Cannot be converted to a number.

I don't want to hard code a bunch of <cfif's in my code.
I would just add a VAL() function, which translates to 0 for nulls.

#NumberFormat(VAL(MyNumber)*100, 000000)#

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.