[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1191
  • Last Modified:

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 Coughter
Brian Coughter
  • 2
1 Solution
<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.
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'))#

I would just add a VAL() function, which translates to 0 for nulls.

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


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now