dcsimg
A Flash Developer Resource Site

Results 1 to 20 of 20

Thread: quick question on how to setprecision in flash

  1. #1
    HACHI-ROKU!!!!
    Join Date
    Dec 2003
    Location
    Moreno Valley, CA
    Posts
    31

    quick question on how to setprecision in flash

    hey, can anyone show me how to setprecision in flash mx 2004?

    This is what I want to do:
    I want to show the mileage of a car climb as you progress... but the only problem is that it shows the decimals too, and all I want it to do is show the decimals to the tenth place, but it keeps jumping and showing it to the thousandths and so forth.

    example: I want the text to display like this 1.8 and not like this 1.899999999999999999999


    I know that setprecision is C++ but can it be done in Flash? can anyone help?
    Life is what you make it!

  2. #2
    you could do
    code:

    mynum=1.899999999999999999999
    mynum=math.floor(mynum*10)/10


  3. #3
    SaphuA SaphuA's Avatar
    Join Date
    Oct 2002
    Location
    The Netherlands
    Posts
    2,182
    Originally posted by xMCNUGGETx
    you could do
    code:

    mynum=1.899999999999999999999
    mynum=math.floor(mynum*10)/10

    I would choose Math.round

    code:

    var badNum = 1.899999999999999999999
    var goodNum = Math.round(badNum*10)/10


  4. #4
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    Why? Round would bring errors when the numbers have fractions greater than .5 in them. Floor is the correct way to do this.

  5. #5
    SaphuA SaphuA's Avatar
    Join Date
    Oct 2002
    Location
    The Netherlands
    Posts
    2,182
    Offcourse not...

    Here are some samples:
    Code:
    45.216 Start nr.
    452.16 *10
    452    Math.round
    45.2   /10
    
    93.688 Start nr.
    936.88 *10
    937    Math.round
    93.7   /10

  6. #6
    Senior Member TeroLebe's Avatar
    Join Date
    Mar 2003
    Location
    Lahti, Finland
    Posts
    302
    no no SaphuA....
    iopred is right in this one.
    hmoob-ky said:
    example: I want the text to display like this 1.8 and not like this 1.899999999999999999999


    example
    Math.round(1.89*10)/10 --> 1.9
    Math.ceil(1.89*10)/10 --> 1.9
    Math.floor(1.89*10)/10 --> 1.8 THIS LOOKS RIGHT!
    int(1.89*10)/10 --> 1.8 so does this

    and negative ones
    Math.round(-1.89*10)/10 --> -1.9
    Math.ceil(-1.89*10)/10 --> -1.8
    Math.floor(-1.89*10)/10 --> -1.9
    int(-1.89*10)/10 --> -1.8

  7. #7
    avatar free
    Join Date
    Jul 2002
    Location
    UK
    Posts
    835
    I don't think you quite understand, Saph.

    Round ( and floor and ceil ) give an integer number. Hmoob-ky wants to have decimal precision, but only outputted to a certain number number of decimal points. A decimal point isn't an error.

    EDIT:
    Hmm, okies, maybe me no understand anyfink ok? Previous poster beat me to it and with a better answer. Examples... why didn't I think of that. Oh well. I really shouldn't post straight after an exam only having 4 hours sleep... bleh.
    Last edited by jonmack; 05-20-2004 at 06:33 AM.
    jonmack
    flash racer blog - advanced arcade racer development blog

  8. #8
    SaphuA SaphuA's Avatar
    Join Date
    Oct 2002
    Location
    The Netherlands
    Posts
    2,182
    Oh wait...

    But then the name of the threat is wrong, using the Math.floor function is less precise then using Math.round!

    What hmoob-ky wants is to chop of all decimals, except the first one. Didn't catch that, sorry.

  9. #9
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    setPrecision is a function in C++ which changes the way all double/floats are displayed when sent to the output stream (eg. the screen), saying how many numbers are used to display the number, it doesnt round off the end, merely displaying a specified number of decimal points.

  10. #10
    HACHI-ROKU!!!!
    Join Date
    Dec 2003
    Location
    Moreno Valley, CA
    Posts
    31
    thanks for the help guys....

    Math.floor worked for a while, until it got to 6, then it started to display 6.199999999999 until it got to 9.19999999999, then after that it went back to showing 10.1

    here is the coding that I used:

    tellTarget ("_root.tach") {
    var badNum = 0.1;
    var goodNum = Math.log(badNum*10)/10;
    milage += goodNum;
    }

    if anyone can help it'll be great.
    Life is what you make it!

  11. #11
    SaphuA SaphuA's Avatar
    Join Date
    Oct 2002
    Location
    The Netherlands
    Posts
    2,182

    :)

    Hey,
    Could you explain what's wrong? The code the guys posted works how it should do. Isn't there someone else wrong in your code?

    PS: Shouldn't you stop using Flash 4 syntax?

  12. #12
    Ihoss
    Guest
    as aphua said, dont use tellTarget

    im not sure why your using log though. log is the logarithm of a number, ie how many times does 2 have to be mulitplied with itself to get 8?

    if milage is wrong, then round it too. this is the code that i would use:
    code:

    _root.tach.badNum=0.1;
    _root.tach.goodNum=Math.int(_root.tach.badNum*10)/10;
    _root.tach.milage+=_root.tach.goodNum;
    _root.tach.milage=Math.int(_root.tach.milage*10)/10;


    I see that you have 2k4 and int might not work, so use floor instead

  13. #13
    jtnw's Avatar
    Join Date
    May 2002
    Posts
    1,328
    Ihoss, It's not Math.int(), but simply 'int()'.

    Also, I'd recommend using 'int()' since 'Math.floor()' will give incorrect results with negative numbers.
    ie:
    code:
    trace(int(-5.23456*10)/10);//-5.2
    trace(Math.floor(-5.23456*10)/10);//-5.3



    jtnw

  14. #14
    Ihoss
    Guest
    thanks for the correction jtnw (i cant believe how a name with 4 letters can take so long to pronounce). but int is depriciated in flash mx so i wasnt sure if it was in 2k4.

  15. #15
    jtnw's Avatar
    Join Date
    May 2002
    Posts
    1,328
    I don't think int is depriciated because as far as I know, there isn't anything that replaces it. int returns the integral part of a number, in other words, everything to the left of the decimal point (This is actually called truncating). Math.floor, however, returns the integer that is closest to negative infinity.

    jtnw

    PS, ya, my name does get annoying to pronounce, but for some reason, I've come to pronounce it as jit-new (don't ask).

  16. #16
    Ihoss
    Guest
    i searched for int in the function list in flash and i got this:
    int

    Availability

    Flash Player 4. This function has been deprecated in Flash 5 in favor of the Math.round method.

    Usage

    int(value)

    Parameters

    value A number to be rounded to an integer.

    Returns

    Nothing.

    Description

    Function; converts a decimal number to the closest integer value.

    See also

    Math.floor

  17. #17
    jtnw's Avatar
    Join Date
    May 2002
    Posts
    1,328
    That's kind of odd, since Math.round does something completely different.

    Oh well, I'm still in favor of using the fastest solution, even if it's Flash 4 script. Since int is faster, I use it because I don't see a reason not to.

    Each to his own,

    jtnw

  18. #18
    Ihoss
    Guest
    i agree. I use random(56); too, as its a lot faster than Math.floor(Math.random()*56);. I dont see why MM takes away these functions, as their quite useful.

  19. #19
    jtnw's Avatar
    Join Date
    May 2002
    Posts
    1,328
    MM doesn't actually take the functions away, they just strongly advise against them. I'm sure int will still work in Flash Player 26, maybe not just in the IDE.

    jtnw

  20. #20
    HACHI-ROKU!!!!
    Join Date
    Dec 2003
    Location
    Moreno Valley, CA
    Posts
    31
    oops, my bad... i had that log there because i started experiment with it... Thanks for the input guys. I finally got it working the way I wanted to.
    Life is what you make it!

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