A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: Text Fields - displaying currency

  1. #1
    Junior Member
    Join Date
    Jan 2001
    Posts
    13
    Hi again. I've got a shopping basket function that I would like to be able to display numbers to the 2nd decimal place.

    In this shopping page I've gotten everything to work, from a "add to basket" button right to a screen that tallys up all what is in the basket. The only problem is when the total price is displayed, it only comes up to 1 decimal place.

    Products that total $10.50 comes up as 10.5

    Not really good when you want to impress someone with a really nice looking animated site that can't even display money properly!

    Anyway, if anyone knows how to get flash to display numbers to 2 decimal places I would be very greatful. Thanks in advanced.

  2. #2
    Member
    Join Date
    Mar 2001
    Location
    Venice, CA
    Posts
    88
    I went through this once. I'll look for the file and if I find it, I'll post the code.

    Anyway, as I recall (and I hope there's a much, much easier way to do this in Flash 4), what I did was:

    Looped through the entire price and created a variable that tracked how many characters there were after the decimal point (created a variable called /:afterPoint and then ran an MBLength on it)

    If the number of characters after the decimal was 1, I tacked on a zero to the price variable.

    If the number was greater than two, I counted how many digits there were before the decimal point and constructed the price variable like so:
    /:price = /:beforePoint & "." & MBSubstring(/:afterPoint,(MBLength(/:beforePoint) + 1),2)

    Note that the actionscript is from memory so the syntax might be off - hopefully the idea is there though.

    I hope that at least gets you started!

    Katharine

    [Edited by katharine on 03-18-2001 at 11:36 PM]

  3. #3
    Junior Member
    Join Date
    Jan 2001
    Posts
    13

    thanks

    Ok, thanks for that.... geez, I was hoping it was a little more simpler than that!

    I don't think I'll need the later part of the code, all the prices are either to the dollar or 50 cents.

    So all I have to do is create a variable that checks how many characters (if any) are after the decimal place. If there is (it will be one ".5") then tack on a "0" at the end of it. Great! Easy... only, how do I "tack" on a number at the end?

  4. #4
    Member
    Join Date
    Mar 2001
    Location
    Venice, CA
    Posts
    88
    Well, it was bothering me so I decided to write out the code. I was way off, incidentally, on what I posted above. Stupid short term memory!!!

    Anyway, this may be more than you need if they're all in .50 increments, but I have to go to bed. You can just eliminate any code you don't need.

    Example:
    http://www.greenbug.com/tutorials/flash/price.html

    And the source:
    http://www.greenbug.com/tutorials/flash/price.zip

    Okay, good night, and good luck!

    Best,
    Kate

  5. #5
    Member
    Join Date
    Mar 2001
    Location
    Venice, CA
    Posts
    88
    P.S. If someone comes along and says "hey, all you have to do is just modify the Int() like so", I will break down and cry
    P.P.S. well not really

  6. #6
    Junior Member
    Join Date
    Jan 2001
    Posts
    13
    .... my god that is more code than I got already in the shopping page!!!!!! Anyway, it will definately help me though, I can pull it apart and see which part exactly I need. Thanks heaps for the file!!

  7. #7
    Junior Member
    Join Date
    Jan 2001
    Posts
    13
    ooo, I'm really sorry Katharine but what you gave me actually doesn't help me. (DAMN!)

    It's great if I wanted to round PI to 2 decimal places, but all I want to do is display 10.5 as 10.50

    On your movie, I tested this, and when I entered 10.5 it gave me $10.5 .... so you see my problem here. Its ok, I will hopefully find something within your nice flash file I got, thanks again.

  8. #8
    Junior Member
    Join Date
    Jan 2001
    Posts
    13

    maybe this...

    Ok, I've had another good look over your code, and I might be able to use something from it. Before I go on however, I should warn you and everyone else that I am very bad at action scripting. I tend to find the simpliest bit of code that does basically what I want and work around that. I don't usually go on a full on coding frenzy, becuase I don't know that much about it.

    With that said, I'll post my idea:

    All I need to be able to do is somehow detect if there is a decimal place in the resulting cost. Then if there is, I can just tack on a "0" at the end of it. (all my prices are in either whole dollars or 50 cent increments, so I will not need anymore information than this) The only thing is, and this is where my lack of Actionscripting knowledge comes in) I don't know how to get flash to detect the decimal place.

    So if anyone can help me, I would be again greatly appriciated. BTW, thanks heaps again to Katharine for her fast help so far!

  9. #9
    Junior Member
    Join Date
    Mar 2001
    Posts
    9
    Arkifarquar

    To display numbers to two decimal places you can use the 'length' element. It works out the number of characters in a variable or string, for example length("arkifarquar") would give 11.

    So, to solve your problem, the code you will need will be something like this (where the 'salevalue' variable is your monetary value):

    if (length(salevalue)=3) {
    salevalue = salevalue+"0";
    }

    The first line works out if the salevalue is only 3 characters long (eg 1.5). If it is, it adds a zero to the end (eg 1.5 turns into 1.50); if not (ie if the salevalue is 4 characters long and therefore correct) if does nothing else.

    I hope that makes some sense.

    It works, as I had the same problem this morning and have now got it sorted :-) Unfortunately I now have a headache instead.

    Cheers

    Tim

  10. #10
    Junior Member
    Join Date
    Mar 2001
    Posts
    9
    Originally posted by Atomic Soda
    if (length(salevalue)=3) {
    salevalue = salevalue+"0";
    }
    Sorry, bit of an error there. It should read:

    if (length(salevalue)==3) {
    salevalue = salevalue+"0";
    }

  11. #11
    Member
    Join Date
    Mar 2001
    Location
    Venice, CA
    Posts
    88
    The issue I see with doing it that way is that it doesn't scale to prices larger than 3 digits (does it?). You could write one for as many odd numbered figures (actually even numbered if you're including the demimal point) as you think you'll need, but that makes re-using the code difficult (I'm a big fan of the reduce-reuse-recycle theory).

    On my way into work this morning, I realized there was a flaw in the code I gave you last night. I didn't account for a sum of, say 10 (rather than 10.00). Anyway I'll revise and re-post. I'll try to do a shorter one that only deals with .50, but in truth, if it were me I'd use something that accounts for as many instances as possible, since specifying .50 is only going to cut out a few lines of code.

    Anyway, I'll try to make it more clear - I've had some coffee now, should help


  12. #12
    Member
    Join Date
    Mar 2001
    Location
    Venice, CA
    Posts
    88
    Okay. I found two errors in the code, here's the new code:
    Code:
          On (Release)
          Set Variable: "/:priceLength" = MBLength(/:price)
          Set Variable: "n" = "0"
          Loop While (n < /:priceLength)
                Set Variable: "n" = n + 1
                Set Variable: "currentChar" = MBSubstring ( /:price, n, 1 )
                If (currentChar ne "." and donePre ne "yes")
                      Set Variable: "beforePrice" = beforePrice & currentChar
                Else If (currentChar eq ".")
                      Set Variable: "donePre" = "yes"
                Else
                      Set Variable: "afterPrice" = afterPrice & currentChar
                End If
          End Loop
          Comment: ===================
          Comment: format dollars (pounds, whatever)
          If (beforePrice eq "" or beforePrice eq ".")
                Set Variable: "beforePrice" = "0"
          End If
          Comment: ===================
          Comment: format cents
          Set Variable: "afterPriceLen" = MBLength(afterPrice)
          If (afterPriceLen = "" or afterPriceLen = 0)
                Set Variable: "afterPrice" = "00"
          Else If (afterPriceLen = 1)
                Set Variable: "afterPrice" = afterPrice & "0"
          Else
                Comment: round up if the decimal warrants it
                If (MBSubstring ( afterPrice, 3, 1 ) > 4)
                      Set Variable: "afterPrice" = MBSubstring ( afterPrice, 1, 2 )
                      Set Variable: "afterPrice" = afterPrice + 1
                Else
                      Set Variable: "afterPrice" = MBSubstring ( afterPrice, 1, 2 )
                End If
          End If
          Set Variable: "displayPrice" = "$" & beforePrice & "."& afterPrice
    End On
    What I changed:

    If (afterPriceLen = "" or afterPriceLen = 0)
    Set Variable: "afterPrice" = "00"
    This accounts for the input price being a whole number.

    Also, when tacking on the 0, I was using the wrong variable (afterPrice rather than afterPriceLen) - my bad.

    I know it's a lot but I hope it helps. Will post source soon, must go to meeting now...

    CODE fix if you need it (above!) (last time I promise!)
    [Edited by katharine on 03-19-2001 at 07:39 PM]

  13. #13
    Junior Member
    Join Date
    Mar 2001
    Posts
    9
    Katharine

    You are quite right - if the value is 11.5 then that's four characters, but so is 1.50 which is correct. So it looks like the idea of locating the decimal point is the best one.

    Back to the drawing board!

    Cheers

    Tim

  14. #14
    Member
    Join Date
    Mar 2001
    Location
    Venice, CA
    Posts
    88
    Okay. Redid it:

    Example:
    http://www.greenbug.com/tutorials/flash/price.html

    And the source:
    http://www.greenbug.com/tutorials/flash/price.zip

    I think I caught all of my errors from last night - give it a spin and let me know.

  15. #15
    Member
    Join Date
    Mar 2001
    Location
    Venice, CA
    Posts
    88
    Now that I have a little time, I thought a little explanation on the code might be helpful to you, Arkifarquar.

    Let's assume that you have a dollar amount stored in a variable called "price".

    Figure out how many characters (including the decimal point) are in your price:

    Code:
          Set Variable: "/:priceLength" = MBLength(/:price)
    Now you're going to loop through each character in price and check what it is:

    Code:
          Set Variable: "n" = "0"
          Loop While (n < /:priceLength)
                Set Variable: "n" = n + 1
                Set Variable: "currentChar" = MBSubstring ( /:price, n, 1 )
    Then you're going to act based on what character it is you're dealing with. You'll create two variables:
    - "beforePrice", which is the first chunk of your price including the decimal point, and
    - "afterPrice", which is the amount of cents (or nothing "")

    Code:
                If (currentChar ne "." and donePre ne "yes")
                      Set Variable: "beforePrice" = beforePrice & currentChar
                Else If (currentChar eq ".")
                      Set Variable: "beforePrice" = beforePrice & currentChar
                      Set Variable: "donePre" = "yes"
                Else
                      Set Variable: "afterPrice" = afterPrice & currentChar
                End If
    And once we've reached the end of the length of the price, we're done:
    Code:
          End Loop
    Now we need to deal with the afterPrice, since it's separate from the rest of the figure.

    Let's check the length:
    Code:
    Set Variable: "afterPriceLen" = MBLength(afterPrice)

    If afterPrice's length = nothing, that means that we have a number like 10, so we want to add a .00 to it
    Code:
          If (afterPriceLen = "")
                Set Variable: "afterPrice" = ".00"
          Else If (afterPriceLen = 0)
                Set Variable: "afterPrice" = "00"
    If afterPrice is 1 digit, then we know we need to tack a zero onto it:
    Code:
          Else If (afterPriceLen = 1)
                Set Variable: "afterPrice" = afterPrice & "0"
    Now it gets really fun - what happens if our sum is longer than 2, such as 10.5356? I know this isn't your case, but it is something to bear in mind. We'll have to chop the afterPrice off at 2, being sure to round up if it needs to be rounded:
    Code:
          Else
                Comment: round up if the decimal warrants it
                If (MBSubstring ( afterPrice, 3, 1 ) > 4)
                      Set Variable: "afterPrice" = MBSubstring ( afterPrice, 1, 2 )
                      Set Variable: "afterPrice" = afterPrice + 1
                Else
                      Set Variable: "afterPrice" = MBSubstring ( afterPrice, 1, 2 )
                End If
          End If
    Finally! We put together all of our pieces and add a nice "$"

    Code:
          Set Variable: "displayPrice" = "$" & beforePrice & afterPrice
    Okay, I think I'm going to stop obsessing over this now, unless you need help with it, which I'm happy to give. Or maybe someone more succinct should take a stab at this? :)

  16. #16
    Junior Member
    Join Date
    Jan 2001
    Posts
    13

    Thank you very much! :)

    Ok, I now have what I need!!!

    Thank you sooooo much for this. I thought that my idea of just locating the decimal point would be the simplest and best way, but in doing so it wouldn't tack on ".00" to the end of whole numbers and make it look a little unprofessional - not good from the clients view. Anyway, so I tried out your new code Katharine, and it works perfectly, it also tacks on the aformentioned ".00" on whole numbers so it is really practical!

    Thanks again Katharine for your most generous help here and to Atomic Soda as well for providing a well needed 3rd eye here.

    Alright, back to work for me too! I should finish this up, there's a lot of products I got to catalogue and get working! Thanks again!

  17. #17
    Member
    Join Date
    Mar 2001
    Location
    Venice, CA
    Posts
    88
    No problem! Good luck with the rest of the site! I hope you'll post the URL when you're ready!

    Also - and I can't believe I've been completely taken over by this little problem - but I made one little fix to the movie. You don't need it, only makes a difference if you're working with more finicky numbers, but still. If you ever need it, the scripting is much better now... same links as above

    Best, and take care!

    Katharine

  18. #18
    Junior Member
    Join Date
    Jan 2001
    Posts
    13
    thanks for the above links, I got them again... but I don't think the zip file was updated (might be my d/l tho).

    Sorry to bother you again, but I've got another problem with the code.

    ok, I've gotten your code into my file, the thing is that my movie somehow ends up that all prices are "$.00".... I've checked over all the code multiple times, and also gotten some of my work mates to look over it and everything is exactly the same (with the exception to changing some variable names to fit in with my movie). The only thing I can think of is that maybe its because at the end of the mouse release action I have a "goto and play frame" command. I was thinking that this may somehow make the script stop functioning half way through it. Could this be the problem?

    Also, I've done this movie using multiple movie clips, and this code is inside a 3rd level movie clip. Would this also cause some problem?

    Thanks again for putting up with me on this , and I hope you can help me with this small issue

  19. #19
    Member
    Join Date
    Mar 2001
    Location
    Venice, CA
    Posts
    88

    the issue that would not die ;-)

    I'd triple-check the consistency of the variables (the first time the code didn't work was because I confused /:afterPrice and /:afterPriceLen).

    The go to and play sequence might have something to do with it, but it's not very likely. Still, you might want to try moving the calculations to the frame you're going to rather than attaching them to the button. Who knows.

    I would suggest running trace actions on the pertinent variables - price, afterPrice and beforePrice - to make sure the thing is getting them in the first place ...

    I can't imagine why it would return $.00 - even if you enter a blank "" it's supposed to return $0.00 - hmm. Maybe you could post your fla somewhere, or e-mail it to me (if it's < 400K, I'm on a 56K here) - I'd be happy to take a look.

    Sorry this is so painful!

    :) Katharine

  20. #20
    Member
    Join Date
    Mar 2001
    Location
    Venice, CA
    Posts
    88
    Wait!!! If it's in an MC then some of my lazy path coding might be tripping you up. Some of the variables specify the root movie (/:syntax) and some do not (syntax). Try switching all the variables to /:variableName syntax. Might help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center