2014-10-04 67 views
0

我有2位小數的一個問題,我想這是35.99美元,而不是美元3599.2價格(PHP)缺少小數

時,我不停地刷新我的網頁用於測試目的發生此問題。有時價格沒有顯示小數位,但在繼續測試我的頁面後恢復正常。

有誰知道這是爲什麼?
下面是我的PHP代碼:

// format for currency values 
function cartweaverMoney($theNum) { 
    $cwLocaleInfo = localeconv(); 
    $cwLocaleInfo["currency_symbol"] = 'USD '; 
    $retStr = number_format($theNum, 
        2, 
        $cwLocaleInfo["mon_decimal_point"], 
        $cwLocaleInfo["mon_thousands_sep"]); 
    if ($cwLocaleInfo["p_cs_precedes"]) { 
     if ($cwLocaleInfo["p_sep_by_space"]) { 
      $retStr = " ".$retStr; 
     } 
     $retStr = $cwLocaleInfo["currency_symbol"].$retStr; 
    } else { 
     if ($cwLocaleInfo["p_sep_by_space"]) { 
      $retStr .= " "; 
     } 
     $retStr .= $cwLocaleInfo["currency_symbol"]; 
    } 
    return $retStr; 
}  
+1

你一定要檢查什麼在這兩個變量$ cwLocaleInfo [ 「mon_decimal_point」], $ cwLocaleInfo [ 「mon_thousands_sep」],以確保您擁有有效seperaters等 – KyleK 2014-10-04 04:50:38

+0

我想你是指這個代碼?我不是用php的familiart,但我發現這個代碼具有相同的php頁面頭文件// LOCAL SETTINGS爲js貨幣格式 – Andy8333 2014-10-05 04:18:00

+0

$ _ENV [「application.cw」] [「currencyPrecedes」] = false; \t \t $ _ENV [「application.cw」] [「currencySymbol」] =「」; \t \t $ _ENV [「application.cw」] [「currencyDecimal」] =「。」; \t \t $ _ENV [「application.cw」] [「currencyGroup」] =「,」; \t \t $ _ENV [「application.cw」] [「currencySpace」] =「」; – Andy8333 2014-10-05 04:20:44

回答

0

localeconv()之前,你必須使用 '的setlocale':

setlocale(LC_ALL, "en_US"); 
$locale_info = localeconv(); 

http://php.net/manual/it/function.localeconv.php

或者,也許,檢查這個代碼: http://php.net/manual/it/function.number-format.php

<?php 
$var = number_format(2234,2); 
$var = number_format($var,2); 
echo $var; 
# Expected Output: 2,234.00 
# Actual Output: 2.00 
?> 

//To fix, remove the thousands separator by setting the decimal point and thousands separator parameters like so: 

<?php 
$var = number_format(2234,2,'.',''); 
$var = number_format($var,2,'.',''); 
echo $var; 
# Expected Output: 2234.00 
# Actual Output: 2234.00 
?> 

享受你的代碼!

編輯: 我在Windows系統中打印localconv()數組返回,它是無效的。 setloca

le(LC_ALL, "en_US"); 
$cwLocaleInfo = localeconv(); 
function print_locale_conv($locale_info) { 
    echo "<pre>\n"; 
    echo "--------------------------------------------\n"; 
    echo " Informazioni sulla configurazione monetaria locale: \n"; 
    echo "--------------------------------------------\n\n"; 

    echo "int_curr_symbol: {$locale_info["int_curr_symbol"]}\n"; 
    echo "currency_symbol: {$locale_info["currency_symbol"]}\n"; 
    echo "mon_decimal_point: {$locale_info["mon_decimal_point"]}\n"; 
    echo "mon_thousands_sep: {$locale_info["mon_thousands_sep"]}\n"; 
    echo "positive_sign:  {$locale_info["positive_sign"]}\n"; 
    echo "negative_sign:  {$locale_info["negative_sign"]}\n"; 
    echo "int_frac_digits: {$locale_info["int_frac_digits"]}\n"; 
    echo "frac_digits:  {$locale_info["frac_digits"]}\n"; 
    echo "p_cs_precedes:  {$locale_info["p_cs_precedes"]}\n"; 
    echo "p_sep_by_space: {$locale_info["p_sep_by_space"]}\n"; 
    echo "n_cs_precedes:  {$locale_info["n_cs_precedes"]}\n"; 
    echo "n_sep_by_space: {$locale_info["n_sep_by_space"]}\n"; 
    echo "p_sign_posn:  {$locale_info["p_sign_posn"]}\n"; 
    echo "n_sign_posn:  {$locale_info["n_sign_posn"]}\n"; 
    echo "</pre>\n"; 
} 
?> 

所以我已閱讀本: http://php.net/manual/it/function.setlocale.php

,我必須改變參數localeconv():

$lang = "us_US"; 
$language = 'english'; 
$codeset = "UTF8"; // warning ! not UTF-8 with dash '-' 

date_default_timezone_set('Europe/Rome'); 
setlocale(LC_ALL, array($lang.'.'.$codeset,$lang.'@euro',$lang,$language)); 

$cwLocaleInfo = localeconv(); 

print_locale_conv($cwLocaleInfo); 

惠特這些作品!

在* nix系統嘗試使用此:

$lang = "us_US"; 
$language = 'english'; 
$codeset = "UTF8"; // warning ! not UTF-8 with dash '-' 
putenv('LANG='.$lang.'.'.$codeset); 
putenv('LANGUAGE='.$lang.'.'.$codeset); 
+0

我試圖添加這個>> _italic setlocale(LC_ALL,「en_US」)<< before >> $ cwLocaleInfo = localeconv(); <<;但它沒有改變任何東西。並且,當我更改$ retStr = number_format($ theNum, 2, $ cwLocaleInfo [「mon_decimal_point」], $ cwLocaleInfo [「mon_thousands_sep」]); to $ retStr = number_format($ theNum,2,'。','');千位分隔符消失。任何想法? – Andy8333 2014-10-05 04:22:55

+0

感謝您的及時回覆Magicianred,我會稍後再嘗試。 :) – Andy8333 2014-10-09 09:34:17