A Flash Developer Resource Site

Results 1 to 18 of 18

Thread: simple math

Hybrid View

  1. #1
    Junior Member
    Join Date
    Mar 2003
    Posts
    14

    simple math

    could anbody tell em how to get numbers to be showed in theire boxes like 6,00 instead of 6,0 or 6,0000000.

    I have got no idea how this funtion works.I tryed INT and Math.round, gues I'm doing something wrong.

    Thanks. Leo

  2. #2
    Banned
    Join Date
    Apr 2001
    Location
    Montréal, Québec.
    Posts
    25,397
    http://www.flashkit.com/board/showth...5&pagenumber=1

    if you just want to round decimals...

    Here is a simple function that'll round a number of to any decimal place.
    ---------------------------------
    Math.roundTo = function(num,dp){
    var d = Math.pow(10,dp);
    return Math.round(num*(d+0.00000001))/d;
    };
    --------------------------------
    example:
    Math.roundTo(1.2345,2)) //1.23

  3. #3
    Junior Member
    Join Date
    Mar 2003
    Posts
    14

    thanx

    I'm not so briliant yet, I think when I see this code VAR D is to be my variable that will be back to decimals here?

  4. #4
    Junior Member
    Join Date
    Mar 2003
    Posts
    14

    My variable is called Eindtotaal

    This variable will always be somting like a full number like 2 or 4 or 35 but could also be 3.5 or 78.4.

    I need it always to be in this mask 2.00 or 4.00 or 35.00 but also 3.50 or 78.40.

    What would be the code.

    as you can see, I'm just beginning.

  5. #5
    Banned
    Join Date
    Apr 2001
    Location
    Montréal, Québec.
    Posts
    25,397
    Just set that function on the first frame of your movie, and call it up when needed...

    Eindtotaal = 77.2;
    trace(Math.roundTo(Eindtotaal,2));

    Or...

    Eindtotaal = 5;
    Eindtotaal_display = Math.roundTo(Eindtotaal,2);
    trace(Eindtotaal_display);

  6. #6
    Junior Member
    Join Date
    Mar 2003
    Posts
    14
    Tnx, that is clear. Do you also know a way to reset all variables at once, instead having to give code for, in this case all 380 different onces?


    Leo

  7. #7
    Banned
    Join Date
    Apr 2001
    Location
    Montréal, Québec.
    Posts
    25,397
    How about reloading the movie on _level0?

  8. #8
    Junior Member
    Join Date
    Mar 2003
    Posts
    14
    Could I be sending you a file, so you could check what I am doing wrong?

    I am finisching this webpage that must be online next monday, however, money seems to shown as 3,4 euro instead of 3,40 euro and that reload from level 0 doesn't seems to ad to my movie, should I change the whole structure then?

    I know it is something to ask but, well, see what you can do for a newbee.

    Leo

  9. #9

  10. #10
    Banned
    Join Date
    Apr 2001
    Location
    Montréal, Québec.
    Posts
    25,397
    Check your PMs!

  11. #11
    Banned
    Join Date
    Apr 2001
    Location
    Montréal, Québec.
    Posts
    25,397
    Sorry... But you got me confused with the language used!

    In any case, here's the right function you need...

    Code:
    //format a number into specified number of decimal places
    Math.formatDecimals = function (num, digits) {
            //if no decimal places needed, we're done
            if (digits <= 0) {
                    return Math.floor(num);
            }
            //Math floor the number to specified decimal places
            //e.g. 12.3456 to 3 digits (12.346) -> mult. by 1000, round, div. by 1000
            var tenToPower = Math.pow(10, digits);
            var cropped = String(Math.floor(num * tenToPower) / tenToPower);
            //add decimal point if missing
            if (cropped.indexOf(".") == -1) {
                    cropped += ".0";  //e.g. 5 -> 5.0 (at least one zero is needed)
            }
    
            //finally, force correct number of zeroes; add some if necessary
            var halves = cropped.split("."); //grab numbers to the right of the decimal
            //compare digits in right half of string to digits wanted
            var zerosNeeded = digits - halves[1].length; //number of zeros to add
            for (var i=1; i <= zerosNeeded; i++) {
                    cropped += "0";
            }
            return(cropped);
    } //Robert Penner May 2001 - source@robertpenner.com
    Example usage:

    Eindtotaal = 77.956;
    Eindtotaal_display = Math.formatDecimals(Eindtotaal,2);
    trace (Eindtotaal_display);

    As for resetting variables, the easiest would seem to be to reload the movie with something like:

    on(release){
    loadmovieNum("name_of_this.swf", 0);
    }

    If you only want to reset a certain number of variables (like the order... Not the address and whatever else...) then you could maybe create a 2 frame "reset" movie clip, that would hold the list of the variables you want to reset, and just play that movie clip when the reset button is pressed, e.g.

    Frame 1:
    Stop();

    Frame 2:
    _level0.my_var1 = "";
    _level0.my_var2 = "";
    _level0.my_var3 = 0;
    _level0.my_var4 = 0;
    _level0.my_var5 = 0;
    _level0.my_var6 = "";
    // Etc... For all variables you want to reset...

    // Then...
    this.gotoAndStop(1);


    Assuming this reset mc was on stage, with the instance name of "reset1", then from your button...

    on(release){
    _root.reset1.gotoAndStop(2);
    }

  12. #12
    Junior Member
    Join Date
    Mar 2003
    Posts
    14

    Half the way

    The variables are clear.

    But the script you have given me is far too high for my skill. I absolutly hav eno idea how to get this to work.

    I end that Eindtotaal has been given a worth. Still from here only that variable has that worth. I see no place to put in my variable in the script below. If it should be onder num, digits, then how?
    I tried,
    Math.formatDecimals = function (Eindtotaal, 2) {
    and also
    Math.formatDecimals = function ((number(eindtotaal)), 2) {
    What am I missing here, I probebly am searching for my shirt while I have it on, but again, please explain what the code is saying and how to use.

    code:--------------------------------------------------------------------------------
    //format a number into specified number of decimal places
    Math.formatDecimals = function (num, digits) {
    //if no decimal places needed, we're done
    if (digits <= 0) {
    return Math.floor(num);
    }
    //Math floor the number to specified decimal places
    //e.g. 12.3456 to 3 digits (12.346) -> mult. by 1000, round, div. by 1000
    var tenToPower = Math.pow(10, digits);
    var cropped = String(Math.floor(num * tenToPower) / tenToPower);
    //add decimal point if missing
    if (cropped.indexOf(".") == -1) {
    cropped += ".0"; //e.g. 5 -> 5.0 (at least one zero is needed)
    }

    //finally, force correct number of zeroes; add some if necessary
    var halves = cropped.split("."); //grab numbers to the right of the decimal
    //compare digits in right half of string to digits wanted
    var zerosNeeded = digits - halves[1].length; //number of zeros to add
    for (var i=1; i <= zerosNeeded; i++) {
    cropped += "0";
    }
    return(cropped);
    } //Robert Penner May 2001 - source@robertpenner.com
    --------------------------------------------------------------------------------


    Example usage:

    Eindtotaal = 77.956;
    Eindtotaal_display = Math.formatDecimals(Eindtotaal,2);
    trace (Eindtotaal_display);

  13. #13
    Banned
    Join Date
    Apr 2001
    Location
    Montréal, Québec.
    Posts
    25,397
    First of all copy that function (between the lines in blue) in my above post,
    and paste it on the first frame of your movie, for sake of clarity on it's own layer.

    Now, assuming a user as filled in the form, and supposing the total is held in this
    variable named Eindtotaal, rather than displaying that variable,
    I suggest you set up a new variable called Eindtotaal_display
    which will be formated by that function on your first frame.

    So when your total has been calculated and you come up with
    Eindtotaal = 77 euros for example, then use this to format that total:

    Eindtotaal_display = Math.formatDecimals(Eindtotaal,2);

    That's all you do to call the function, you don't set or change anything
    in the function itself, and the above line should output:

    77.00 ...Which is what you wanted.

    Now change your textfield's variable from Eindtotaal to Eindtotaal_display,
    and it should now display the correctly formated value.

    Do the same for any other variable you want to format.
    If you have a totaltaxes variable named Eindtaxestotaal than create a new
    variable Eindtaxestotaal_display, and format it with:

    Eindtaxestotaal_display = Math.formatDecimals(Eindtaxestotaal,2);

  14. #14
    Junior Member
    Join Date
    Mar 2003
    Posts
    14

    !

    I am truly sorry you can't see me dance here.
    This is language I do understand.

    Love U for it grany!!

  15. #15
    Banned
    Join Date
    Apr 2001
    Location
    Montréal, Québec.
    Posts
    25,397
    Grany has the patience of a saint!

  16. #16
    Junior Member
    Join Date
    Mar 2003
    Posts
    14
    honest, you put a lot of time in it.

    greatfull for it.

    Should I be able to help in any way.

    got my adres
    Last edited by breinstein; 03-01-2003 at 06:13 PM.

  17. #17
    Banned
    Join Date
    Apr 2001
    Location
    Montréal, Québec.
    Posts
    25,397
    How about spilling out the cash (in euros if you want!) for a 3 week trip to Tahiti (all included!) for me & the wife?
    Guess it wouldn't be asking for too much, now would it?

    Glad I could help!

  18. #18
    Junior Member
    Join Date
    Mar 2003
    Posts
    14

    full of it

    keep trying. I do make some money on these pages however. If you are able to design code on I higher level as my bul@#$%@$, we might be able to actually make some money, besides the regulair job offcourse.

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