2012-07-26 2 views
2

저는 현재 회사에 수표 인쇄 솔루션을 제공하고 있습니다. 수표를 발행 할 때 지급되는 금액에서 수백만, 수천, 십만, 수천, 수백, 수십 및 단위 (파운드/달러/유로 등)의 수를 인쇄해야합니다.수를 수천, 수백 등으로 나누십시오.

111232.23의 경우 아래에 작성한 코드에서 다음 내용이 올바르게 출력됩니다. 이 일을보다 효율적으로 또는 신뢰할 수있는 방법으로 생각하는 데 도움이되지 않습니까? 누구든지이 작업을 수행하는 라이브러리/클래스 수학 기법을 알고 있습니까?

float(111232.23) 
Array 
(
    [100000] => 1 
    [10000] => 1 
    [1000] => 1 
    [100] => 2 
    [10] => 3 
    [1] => 2 
) 

<?php 

$amounts = array(111232.23,4334.25,123.24,3.99); 

function cheque_format($amount) 
{ 
    var_dump($amount); 
    #no need for millions 
    $levels = array(100000,10000,1000,100,10,1); 
    do{ 
     $current_level = current($levels); 
     $modulo = $amount % $current_level; 
     $results[$current_level] = $div = number_format(floor($amount)/$current_level,0); 
     if($div) 
     { 
      $amount -= $current_level * $div; 
     } 
    }while($modulo && next($levels)); 

print_r($results); 
} 

foreach($amounts as $amount) 
{ 
cheque_format($amount); 
} 
?> 
+0

당신은 PHP에서 소수 자릿수에서 숫자를 분리하고 for 루프를 마지막 위치 (길이)에서 첫 번째 (0)까지 분할 할 수 있다는 것을 알고 있습니까? 나는이 일을 당신의 영혼이 존경하는 동안 나는 그것이 더 빠를 것이라고는 생각하지 않습니다. – konqi

답변

3

나는 PHP가 가지고있는 number_format 함수를 다시 작성했다고 생각합니다. 필자의 제안은 PHP 함수를 다시 작성하는 것이 아니라 PHP 함수를 사용하는 것입니다.

<?php 

$number = 1234.56; 

// english notation (default) 
$english_format_number = number_format($number); 
// 1,235 

// French notation 
$nombre_format_francais = number_format($number, 2, ',', ' '); 
// 1 234,56 

$number = 1234.5678; 

// english notation without thousands separator 
$english_format_number = number_format($number, 2, '.', ''); 
// 1234.57 

?> 
+0

실제 숫자가 필요합니다. 체크 박스의 특정 상자에 넣어야합니다. – Leo

+0

@Leo 그런 다음 PHP 함수를 사용하여 숫자를 에코하지만 원본 데이터는 그대로 유지하십시오. – Fluffeh

2

나는 PHP 스크립트는이에 대한 것이 정확히 모르겠지만, 당신이있는 경우 10000, 1000, 100, 10, 1 일 당신은의 양이 필요합니다. 금액 10,000 달러는 몇 만원입니까?

floor($dollar/10000) 

몇 천이나됩니까?

floor(($dollar%10000)/1000) 

1

이 질문에 대한 답이 아니라, 다음은 또한 소수를 분해.

function cheque_format($amount, $decimals = true, $decimal_seperator = '.') 
{ 
    var_dump($amount); 

    $levels = array(100000, 10000, 1000, 100, 10, 5, 1); 
    $decimal_levels = array(50, 20, 10, 5, 1); 

    preg_match('/(?:\\' . $decimal_seperator . '(\d+))?(?:[eE]([+-]?\d+))?$/', (string)$amount, $match); 
    $d = isset($match[1]) ? $match[1] : 0; 

    foreach ($levels as $level) 
    { 
     $level = (float)$level; 
     $results[(string)$level] = $div = (int)(floor($amount)/$level); 
     if ($div) $amount -= $level * $div; 
    } 

    if ($decimals) { 
     $amount = $d; 
     foreach ($decimal_levels as $level) 
     { 
      $level = (float)$level; 
      $results[$level < 10 ? '0.0'.(string)$level : '0.'.(string)$level] = $div = (int)(floor($amount)/$level); 
      if ($div) $amount -= $level * $div; 
     } 
    } 

    print_r($results); 
} 
+0

매우 좋습니다. + 1 내 편에서 –

관련 문제