donglikuang8145
2017-04-23 10:24
浏览 46
已采纳

如何更改整个wordpress网站的数字格式?

I need to change number format for my whole site as different country uses different formats like

  • United States — $1,234,567.89 USD
  • Canada — $1,234,567.89 CAD
  • Great Britain — £1.234.567,89 GBP
  • European — €1.234.567,89 EUR

By default it uses United State format but i need it to be European.

what i can do is use

number_format($number, 2, ',', '.'); 

this simple function, which works fine for me, but i have to use it every time i am working with number, And i need this to use for once like in wp-config.php or function.php file and that will change the format for whole site.

Is it possible?

图片转代码服务由CSDN问答提供 功能建议

我需要更改整个网站的数字格式,因为不同的国家/地区使用不同的格式,例如 \ n

  • 美国 - $ 1,234,567.89 USD
  • 加拿大 - $ 1,234,567.89 CAD
  • 英国 - £1.234.567,89 GBP
  • 欧洲 - €1.234.567,89 EUR

    默认情况下,它使用美国国家格式,但我需要它是欧洲格式。

    我能做的是使用

      number_format($ number,2,',','。');  
       
     
     

    这个简单的功能,对我来说很好用,但每次我使用数字时都必须使用它,而且我需要使用它一次就好了 在wp-config.php或function.php文件中,这将改变整个站点的格式。

    有可能吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongpin1059 2017-04-23 11:05
    已采纳

    you can use wp filter for prepare common function in which you can define number format for different country.

    apply_filters( 'number_format_i18n', change_number_format );

    number format i18n

    developer number format i18n

    点赞 打赏 评论
  • dongzhuange2625 2017-04-23 11:10

    Assuming that you're using WooCommerce..
    You can create a filter that will modify the price's format and then to load it according to your language/currency.

    function change_product_price_display( $price ) {
        if( $lang == 'EN' ) {
          $price = number_format( $price, 2, ',', '.' ); 
          return $price;
        }
    
        return $price;
    }
    add_filter( 'woocommerce_get_price_html', 'change_product_price_display' );
    add_filter( 'woocommerce_cart_item_price', 'change_product_price_display' );
    
    点赞 打赏 评论
  • doumibi6899 2017-04-23 12:02

    I assume the admin setting of the format is stored in the database. Read that setting and use the values in constants

    $setting = array();
    $setting = get_option('formatsetting');
    const PRECISION = $setting['precision'];
    const DECIMALS  = $setting['decimals'];
    const THOUSANDS = $setting['thousands'];
    

    Then you can use number_format like this:

    number_format($number, PRECISION, DECIMALS, THOUSANDS);
    

    You can extend it to your needs.

    点赞 打赏 评论

相关推荐 更多相似问题