A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: truncating to two decimal places

  1. #1
    Junior Member
    Join Date
    Oct 2000
    Posts
    19
    I've seen a post on here that says how to do it that says to convert the number into a string and then find out where the decimal is and have the actionscript delete the characters after the second decimal place but I dont know how to do that. The numbers vary from no decimal places to like 10 decimal places. If the outcome of a problem has no decimal places I would also need to add the ".00" Thanks

    David

  2. #2
    function fmtNum(num, nad)
    {
    num = num.toString().split(".");
    return parseFloat(num[0] + "." + num[1].substring(0, nad))
    }


    //should give you 999.8712
    num = 999.8712798
    trace(fmtNum(num, 4))

  3. #3
    Junior Member
    Join Date
    Oct 2000
    Posts
    19
    thanks man

  4. #4
    Junior Member
    Join Date
    Oct 2000
    Posts
    19
    it didnt work. it returns a comma instead of a period for some strange reason and it doesnt have two decimal places, it still has the original amount except it has a comma instead of a period

  5. #5
    I have tested it, and it seems to work okay. Are you sure you copied the function correctly? What country is your computer set up in? Some countries use a comma in place of the decimal. how large are the numbers you are using? if the values are above the allowed size for a Number, it will wont work.

    //example
    num1 = 999987979879879879797986
    trace(num1)

    num2 = 999987979879879879797987
    trace(num2)
    trace(num1 == num2) //evaluates "true"

    Can you give me an example of the script you are using?


  6. #6
    Junior Member
    Join Date
    Oct 2000
    Posts
    19
    sure here it is without your code loaded. It calculates KB/sec by sampling it four times and then averaging it. on the actual movie I have dynamic text boxes displaying the resulting values. My computer is set for American numbers. If you want to see the script in action go here:
    http:/www.csc.cc.il.us/david/bandwidth.html
    It will be there for the night but it will be deleted tomorrow. If you connection is too fast it will not work right. It works for 128kb and lower though. Thanks for your help.


    var bytesloaded = _root.getBytesLoaded();
    var totalbytes = _root.getBytesTotal();
    var percent = Math.round((bytesloaded/totalbytes)*100);
    var kilobytes = Math.round(totalbytes/1000);
    var output = percent+"%"+" "+"of"+" "+kilobytes+"kb";
    // starts the counter so that the bytes loaded are sampled every second
    var counter = Math.round(getTimer()/1000);
    // <-------testing begins------------->
    // start the counter at 3 because on slower modems, the counter
    // might get to 3 before all the actionscript is loaded
    if (counter == 3) {
    var test1 = bytesloaded/1024;
    }
    if (counter == 4) {
    var test2 = bytesloaded/1024;
    }
    var speed1 = test2-test1;
    if (counter == 5) {
    var test3 = bytesloaded/1024;
    }
    var speed2 = test3-test2;
    if (counter == 6) {
    var test4 = bytesloaded/1024;
    }
    var speed3 = test4-test3;
    if (counter == 7) {
    var test5 = bytesloaded/1024;
    }
    var speed4 = test5-test4;
    // <---------testing ends-------------->

    var totalspeed = speed1+speed2+speed3+speed4;

    var avgspeed = totalspeed/4;

  7. #7
    Hmmm... Well I took the "results" value [177.216796875] from the bandwidth flash movie and ran it through the fmtNum() function and it spit out [177.21].

    trace(fmtNum(177.216796875, 2)); //177.21


    I don't know why your results show a comma (what timezone is your computer in?).

    Here is another version of fmtNum without using String methods (pure Math/Number methods)

    function fmtNum(num, nad)
    {
    var upper = parseInt(num);
    var lower = num - upper;
    for (var i=0;i<nad;i++) lower *= 10;
    lower = parseInt(lower)
    for (var i=0;i<nad;i++) lower /= 10;
    return lower+upper;
    }


  8. #8
    Junior Member
    Join Date
    Oct 2000
    Posts
    19
    Okay, I'll try the new math version. Actually, I didnt use your function when I tested it, I took the function apart and tried it. I wont be able to test it until tomorrow though because I'm at home now. Wow, you have a fast connection. I never tested it at 177k. I'm just curious though, did the four tests all come out the same? I just tried it from here and they all came out to like 4.7013342. Maybe I shouldnt even do four tests and then the average. Instead I should just do one test. The reason I had four was because the "Show Streaming" function in Flash was returning different values every test. Thanks

    David

  9. #9
    I don't remember them coming out the same. They showed negative numbers in the thousands and went to zero prior to the results screen displaying.

  10. #10
    Junior Member
    Join Date
    Oct 2000
    Posts
    19
    hmmmm....thats strange. It should've started out with negative numbers(because it is dividing a small number by 1024) and then it shouldve leveled out to a positive number as it's final value. thats what it's done for me every time. I wonder how the average came out to 177 if the fields were all zeros. All it is doing is adding the four fields and dividing them by 4. It looks like I have some debugging to do. This is the first time I've heard of this problem though. Thanks for your help.

    David

  11. #11
    Junior Member
    Join Date
    Oct 2000
    Posts
    19
    Thanks dude, the math version works

    David

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