hoony's web study

728x90
반응형


  function formatAsDollars (amount:Number):String
 { 
  // return a 0 dollar value if amount is not valid  // (you may optionally want to return an empty string) 
  if (isNaN(amount))
  { 
  return "$0.00";
  }  
  // round the amount to the nearest 100th
  amount = Math.round(amount*100)/100;  
  // convert the number to a string 
  var amount_str:String = String(amount);  
  // split the string by the decimal point, separating the  // whole dollar value from the cents. Dollars are in 
  // amount_array[0], cents in amount_array[1] 
  var amount_array = amount_str.split(".");  
  // if there are no cents, add them using "00" 
  if (amount_array[1] == undefined)
  {   amount_array[1] = "00";  } 
  // if the cents are too short, add necessary "0" 
  if (amount_array[1].length == 1)
  {   amount_array[1] += "0";  } 
  // add the dollars portion of the amount to an  // array in sections of 3 to separate with commas 
  var dollar_array:Array = new Array(); 
  var start:Number;
  var end:Number = amount_array[0].length; 
  while (end > 0)
  {  
  start = Math.max(end - 3, 0); 
  dollar_array.unshift(amount_array[0].slice(start, end));
  end = start; 
  } 
  // assign dollar value back in amount_array with
  // the a comma delimited value from dollar_array
  amount_array[0] = dollar_array.join(",");
  // finally construct the return string joining  // dollars with cents in amount_array 
  return ("$" + amount_array.join("."));
 }


출처 : http://kb2.adobe.com/cps/142/tn_14267.html
728x90

공유하기

facebook twitter kakaoTalk kakaostory naver band
loading