Number format: Left pad and remove decimal

Posted on 2005-05-11
Last Modified: 2013-12-24
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
Question by:Brian Coughter
    LVL 9

    Expert Comment

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



    Author Comment

    by:Brian Coughter
    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.
    LVL 9

    Accepted Solution

    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'))#

    LVL 15

    Expert Comment

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

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


    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
    Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now