2011-05-03 166 views
3

千位分隔符(如逗號)在printf中如何做?千位分隔符printf

例如:

printf("<td class='number'>%d</td>", $totals['Sold']); //need thousand separated 
printf("<td class='number'>%.2f</td>", $totals['Fees']); //need thousand separated 

UPDATE這是我本來有:

foreach(array_keys($totals) as $key){ 
     if(is_numeric($totals[$key])){ 
      $totals[$key] = number_format($totals[$key],2); 
     } 
    } 

    echo "</tbody> 
     <tfoot><tr>"; 
    echo "<td class='text' colspan='4'>Totals:</td>"; 
    echo "<td class='number'>{$totals['Bought']}</td>"; 
    echo "<td class='number'>{$totals['Sold']}</td>"; 
    echo "<td class='number'>{$totals['Fees']}</td>"; 
    echo "<td class='number'>{$totals['Realized']}</td>"; 
    echo "<td class='number'>{$totals['Net']}</td>"; 
    echo "<td colspan='3'>{$totals['EOD Price']}</td>"; 
    echo "</tr> 
     </tfoot>"; 

,我希望它來是這樣的:

echo "</tbody> 
     <tfoot><tr>"; 
    echo "<td class='text' colspan='3'>Totals:</td>"; 
    printf("<td class='number'>%d</td>", $totals['Bought']) ; 
    printf("<td class='number'>%d</td>", $totals['Sold']) ; 
    printf("<td class='number'>%.2f</td>", $totals['Fees']) ; 
    printf("<td class='number'>%.2f</td>", $totals['Realized']) ; 
    printf("<td class='number'>%.2f</td>", $totals['Net']) ; 
    printf("<td colspan='3'>%.2f</td>", $totals['EOD Price']) ; 
    echo "</tr> 
     </tfoot>"; 

但我需要逗號

+0

這是什麼意思?逗號不是數字,因此使用%d本質上是錯誤的。我認爲你正在尋找的是解析一個數字。 – Zirak 2011-05-03 18:33:12

+0

@Zirak,我想要一些數字用逗號整數,有些浮點數 – Neal 2011-05-03 18:35:35

回答

9

漂亮使您的代碼,不附和這一切。剛剛打破了PHP方面的做,然後代替了printf的使用number_format只是附和:

</tbody> 
<tfoot> 
    <tr> 
     <td ...>Totals</td> 
     <td ...><?php echo number_format($totals['Bought'], 0); ?></td> 
     <td ...><?php echo number_format($totals['Sold'], 0); ?></td> 
     <td ...><?php echo number_format($totals['Fees'], 2); ?></td> 
     ... 
    </tr> 
</tfoot> 
+0

這可能是最好的方法。爲什麼我沒有想到這個? – Neal 2011-05-03 18:42:51

+1

稍微改變一下少一點的打字(儘管有人說它的可讀性較差,所以要做出選擇)是做類似... '<?= number_format($ totals ['Bought'],0 )?>' 這與short_open_tags不一樣,所以不要擔心,如果你有禁用。 – 2015-12-22 10:56:40

0
$totals['Sold']=number_format($totals['Sold']); 
$totals['Fees']=number_format($totals['Fees']); 
1

至於除了ircmaxell的回答是:

如果你有一個多語種的軟件,你不希望通過每次調用時dec_point和thousands_sep參數number_format您可以使用檢查當前的語言環境來判斷當前小數點和千位分隔符字符像這樣的自定義函數:

<?php 
    /** 
    * Calls {@link number_format} with automatically determining dec_point and thousands_app 
    * according to the current locale in case they are null or omitted. 
    * @param float $number The number being formatted. 
    * @param int $decimals [optional] Sets the number of decimal points. 
    * @param string|null $dec_point [optional] Decimal point character (determined automatically 
    *  when set to null or omitted). 
    * @param string|null $thousands_sep [optional] Thousands separator character (determined 
    *  automatically when set to null or omitted). 
    * @return string 
    */ 
    function custom_number_format($number, $decimals = 0, $dec_point = null, $thousands_sep = null) { 
     $locale   = setlocale(LC_NUMERIC, 0); // yes, there is no getlocale() 
     $def_dec_point  = '.'; 
     $def_thousands_sep = ','; 
     switch (substr($locale, 0, 2)) { 
      case 'de': 
       $def_dec_point  = ','; 
       $def_thousands_sep = '.'; 
       break; 
      case 'fr': 
       $def_dec_point  = ','; 
       $def_thousands_sep = ' '; 
       break; 
      case 'en': 
      default: 
       $def_dec_point  = '.'; 
       $def_thousands_sep = ','; 
     } 
     if (!isset($dec_point)) $dec_point = $def_dec_point; 
     if (!isset($thousands_sep)) $thousands_sep = $def_thousands_sep; 

     return number_format($number, $decimals, $dec_point, $thousands_sep); 
    } 

    $nr = 1234.56; 
    setlocale(LC_NUMERIC, 'de_DE'); 
    echo custom_number_format($nr, 2); 
    # output: 1.234,56 

    setlocale(LC_NUMERIC, 'en_US'); 
    echo custom_number_format($nr, 2); 
    # output: 1,234.56 

    setlocale(LC_NUMERIC, 'fr_FR'); 
    echo custom_number_format($nr, 2); 
    # output: 1 234,56 

    # you still can use whatever you want as dec_point and thousands_sep no matter which locale is set 
    setlocale(LC_NUMERIC, 'de_CH'); 
    echo custom_number_format($nr, 2, '.', "'"); 
    # output: 1'234.56