dcsimg
A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Format Dynamic Text Fields

  1. #1
    How do you format dynamic Text Fields in Flash MX? So lets say your getting a number of 450678.9 and I need it to format to $450,678.90 any ideas anyone? thanks in advance..

  2. #2
    Banned
    Join Date
    Apr 2001
    Location
    Montréal, Québec.
    Posts
    25,397

  3. #3
    Senior Member EQFlash's Avatar
    Join Date
    Jun 2002
    Location
    where i'm at
    Posts
    2,735
    you can copy and paste this code;
    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.round(num);
    	}
    	//round 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.round(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);
    };
    
    my_num = Math.formatDecimals(1.1, 5);
    trace(my_num);

  4. #4
    Banned
    Join Date
    Apr 2001
    Location
    Montréal, Québec.
    Posts
    25,397
    Copy cat! Even cleared Penner's copyright did you!

    BTW, this my_num = Math.formatDecimals(1.1, 5); will give you 5 decimals!

  5. #5
    Senior Member EQFlash's Avatar
    Join Date
    Jun 2002
    Location
    where i'm at
    Posts
    2,735
    oldnewbie, you caught me. it's always nice to share

  6. #6

    how about the comma?

    is there a way to get the comma in there? like 4,600? the ,?
    thanks again

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