Skip to main content
Inspiring
May 11, 2007
Answered

Currency Formatting without new/import

  • May 11, 2007
  • 6 replies
  • 1872 views
Background:
  • Running cfmx7

  • CFFORM FORMAT=FLASH

  • I have several fields that are totaled using actionscript, most of these fields are user input, so virtually any dollar amount can be entered


If the total is (for example) 375.00 (or any other whole dollar amount) the form only displays 375 (not 375.00)
LIkewise if the total is (for example) 559.50, the form only displays 559.5

The math is still correct, but the display isn't, we need it to display the trailing zeros properly.

Since with a cfform format=flash, I cannot use the new / import / (or some other functions) within actionscript, the currency formatting scripts I've found are not usable.

I created the following code to "append" the .00 or .n0 onto the end of the amount and display it in the field:

quote:


<CFSAVECONTENT variable="subTotal">
var a = (Number(pmt_qty.text) * Number(pmt_amount.text))+Number(fieldone.text)+Number(fieldtwo.text)+Number(fieldthree.text);
if(!(Number(a)%1)){subtotal.text = Number(a)+".00"}
if(Number(a)%1){subtotal.text = Number(a)}
var b = (Number(a)*10);
if(!(Number(b)%1) && (Number(a)%1)){subtotal.text = Number(a)+"0"}
</CFSAVECONTENT>



HERE IS THE PROBLEM...

This works most of the time, but sometimes it is dropping off the trailing zero.

For example,
pmt_qty = 3
pmt_amount = 62.30
fieldone = 0
fieldtwo = 0
fieldthree = 0

it should display 186.90, but it is displaying 186.9 (trailing zero doesn't work)

BUT
pmt_qty = 2
pmt_amount = 62.30
fieldone = 0
fieldtwo = 0
fieldthree = 0

It should (and does) display 124.60 (trailing zero works)

I know this was rather long-winded, but I'm completely stumped and I'm sure its something small I've missed.

If anyone can lend some assistance, I'd greatly appreciate it!!
    This topic has been closed for replies.
    Correct answer BKBK
    the trailing zeros are still dropped off.
    I've given the code in the Technote a run. I can see that it is inadequate for your purposes. I suppose you are looking for the actionscript equivalent of CFML functions like numberFormat and decimalFormat.

    I can't find any in the literature. So I've written this one. I have assumed you will be using positive numbers.

    <cfform name="myForm" format="flash">
    <cfformitem type="script">
    function roundItOff() {
    var myNumber:Number = myForm.myVar;
    var decimalPlaces:Number = myForm.dec;
    var scaleFactor:Number = Math.pow(10,decimalPlaces);

    var scaledNumber = Math.round(myNumber*scaleFactor);
    var wholePart = Math.floor(scaledNumber/scaleFactor);
    var decimalPart = Math.round((scaledNumber/scaleFactor - wholePart)*scaleFactor);
    // append decimal part to arbitrary string of zeros
    var decimalFormattingString:String = "0000000000" + decimalPart.toString();
    var startingPoint = decimalFormattingString.length - decimalPlaces;
    var endPoint = decimalFormattingString.length;
    // extract decimal part
    var formattedDecimalPart = decimalFormattingString.substring(startingPoint,endPoint);
    var result = 'formatted number: ' + wholePart + '.' + formattedDecimalPart;
    alert(result);
    }
    </cfformitem>
    <cfinput type="text" name="myVar" label="Enter number to round off" required="Yes" >
    <cfinput type="text" name="dec" label="Enter number of decimal places" required="Yes" >
    <cfinput type="button" name="btn" value="show number as decimal" onclick="roundItOff()" >
    </cfform>



    6 replies

    May 30, 2007
    Thank you for that! I had been looking for some time! I am new to Actionscript 2 and I was surprised it was not a built in function for CFForm and Flash)!
    BKBK
    Community Expert
    Community Expert
    May 14, 2007
    !
    VelariaAuthor
    Inspiring
    May 14, 2007
    With only some slight modificatiosn (so that it fit my application/usage) this worked!!

    Thanks so much for your help BKBK, I really appreciate it! (and hopefully this can help others too!)

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    May 13, 2007
    the trailing zeros are still dropped off.
    I've given the code in the Technote a run. I can see that it is inadequate for your purposes. I suppose you are looking for the actionscript equivalent of CFML functions like numberFormat and decimalFormat.

    I can't find any in the literature. So I've written this one. I have assumed you will be using positive numbers.

    <cfform name="myForm" format="flash">
    <cfformitem type="script">
    function roundItOff() {
    var myNumber:Number = myForm.myVar;
    var decimalPlaces:Number = myForm.dec;
    var scaleFactor:Number = Math.pow(10,decimalPlaces);

    var scaledNumber = Math.round(myNumber*scaleFactor);
    var wholePart = Math.floor(scaledNumber/scaleFactor);
    var decimalPart = Math.round((scaledNumber/scaleFactor - wholePart)*scaleFactor);
    // append decimal part to arbitrary string of zeros
    var decimalFormattingString:String = "0000000000" + decimalPart.toString();
    var startingPoint = decimalFormattingString.length - decimalPlaces;
    var endPoint = decimalFormattingString.length;
    // extract decimal part
    var formattedDecimalPart = decimalFormattingString.substring(startingPoint,endPoint);
    var result = 'formatted number: ' + wholePart + '.' + formattedDecimalPart;
    alert(result);
    }
    </cfformitem>
    <cfinput type="text" name="myVar" label="Enter number to round off" required="Yes" >
    <cfinput type="text" name="dec" label="Enter number of decimal places" required="Yes" >
    <cfinput type="button" name="btn" value="show number as decimal" onclick="roundItOff()" >
    </cfform>



    VelariaAuthor
    Inspiring
    May 14, 2007
    That might just do the trick!! I'm going to give it a try with my code this morning and will post back here and let you know.

    THANKS!!!!
    BKBK
    Community Expert
    Community Expert
    May 13, 2007
    it still doesn't work
    No? I'll give it a run.



    BKBK
    Community Expert
    Community Expert
    May 12, 2007
    To get one decimal place, multiply by 10 and divide by 10. To get two decimal places, multiply by 100 and divide by 100, and so on. You will find more in this Technote about rounding to specific decimal places in Flash

    That should sound familiar to you. I have just quoted it from a thread that you started some weeks ago

    VelariaAuthor
    Inspiring
    May 12, 2007
    True that, it does sound familiar, but it still doesn't work. :)

    That will calculate decimal places, no doubt, however, the trailing zeros are still dropped off. I don't need to calculate the decimal places, the decimal is in the right spot...I need to get the trailing zeros (any zeros after the decimal) to display once the value is placed back into the form field subtotal.

    Thanks though! :)