A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: limit the decimal cases

  1. #1
    Junior Member
    Join Date
    May 2001
    Posts
    19
    Hi,

    I need a function that limit the decimal cases displayed in a dinamic text box, as the example:

    "1,87939849343" to "1,87"

    thanks

  2. #2
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    I take it English isn't your native language, du to spelling and the , instead of . (Where all does that? I know Germany does). Anyway, heres a function:

    Code:
    dec = 345.4343433453;
    
    function decLen(f,n){
    	var f = String(f);
    	var c = f.slice( 0,f.indexOf(".",0)+n+1);
    	return Number(c);
    };
    
    Ndec = decLen(dec,2); // = 345.43

  3. #3
    Member
    Join Date
    Jan 2002
    Location
    Venice, CA
    Posts
    77
    or a quicker, one-line way to do it is:

    (assuming "myNumber" is the number of which you want to limit the displayed digits.)

    myNumber= int(myNumber*100)/100;

    this basically will multiply the target number by 100 (multiplying by 100 will move the decimal point two digits to the right), then convert the number to an integer (hacking off everything after the decimal point), and then divides the number by 100 (thus moving the decimal point back two digits to the left) and leaving you with a happy semi-truncated floating-point number.

    snarf,
    brian.

  4. #4
    Gross Pecululatarian Ed Mack's Avatar
    Join Date
    Dec 2001
    Location
    UK
    Posts
    2,954
    You dorty mind reader!

    I just came back here, having had the same thought, only ot find you beat me too it

  5. #5
    Senior Member RazoRmedia's Avatar
    Join Date
    Oct 2000
    Location
    UK
    Posts
    3,016
    I needed something recently to literally have three characters only, regardless of the decimal point: ie

    2.34
    3.45
    4.56 etc etc

    this is what I came up with:

    Code:
    short = long.toString( radix ).substr(0,3);

    also, this can be adapted for for timers (milliseconds timers, for example, where you may simply want the last 3 digits of a number

    ie:
    45565456
    45565457
    45565458
    45565459

    Code:
    short = long.toString( radix ).substr(-3,3);

  6. #6
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Just wanted to point out, that none of those work, if you need to round up numbers to certain decimal point, not just throw away all the rest

  7. #7
    Senior Member RazoRmedia's Avatar
    Join Date
    Oct 2000
    Location
    UK
    Posts
    3,016
    Originally posted by tonypa
    Just wanted to point out, that none of those work, if you need to round up numbers to certain decimal point, not just throw away all the rest
    this does:

    myNumber= math.round((myNumber*100)/100);

  8. #8
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    I suppose you ment:

    myNumber = math.round((myNumber*100))/100;


    Lets see:
    myNumber = 123.45499999
    rounds with your code to: 123.45

    I might be mistaken, but I think it should be 123.46

  9. #9
    Senior Member RazoRmedia's Avatar
    Join Date
    Oct 2000
    Location
    UK
    Posts
    3,016
    no it shouldn't, 123.45499999 rounded to 2 decimal places equals 123.45

  10. #10
    Junior Member
    Join Date
    Mar 2002
    Posts
    8
    I need to something slightly different.

    need to go from
    1.1234567890e-123 to 1.12e-123

    I know you can chop off the exponent and the first 3 digit than put them back, but is there an easier way?


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