Title: Problems with function apply_rounding_rules()
Last modified: August 22, 2016

---

# Problems with function apply_rounding_rules()

 *  Resolved [Gariest](https://wordpress.org/support/users/gariest/)
 * (@gariest)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/problems-with-function-apply_rounding_rules/)
 * Hello I’ve recently discovered problems with this function. Especially when you’re
   using it with convert_price_amount() like _on line 832 in multi-currency class_
   Problem is that you’ve passed price with commas and all special characters and
   then ceil() or floor() doesn’t work.
    Here is a little function, for removing
   all characters except numbers and decimals dot. Feel free to use it.
 *     ```
       function float_number($num) {
           $dotPos = strrpos($num, '.');
           $commaPos = strrpos($num, ',');
           $sep = (($dotPos > $commaPos) && $dotPos) ? $dotPos :
               ((($commaPos > $dotPos) && $commaPos) ? $commaPos : false);
   
           if (!$sep) {
               return floatval(preg_replace("/[^0-9]/", "", $num));
           } 
   
           return floatval(
               preg_replace("/[^0-9]/", "", substr($num, 0, $sep)) . '.' .
               preg_replace("/[^0-9]/", "", substr($num, $sep+1, strlen($num)))
           );
       }
       ```
   
 *  Hope this helps. Thank you.
 * [https://wordpress.org/plugins/woocommerce-multilingual/](https://wordpress.org/plugins/woocommerce-multilingual/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [Gariest](https://wordpress.org/support/users/gariest/)
 * (@gariest)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/problems-with-function-apply_rounding_rules/#post-5468488)
 * So, now this function looks like this
 *     ```
       function apply_rounding_rules($price, $currency = false ){
   
               global $woocommerce_wpml;
   
                $price= $this->float_number($price);
   
               if( !$currency )
   
               $currency = $this->get_client_currency();
   
               $currency_options = $woocommerce_wpml->settings['currency_options'][$currency];
   
               if($currency_options['rounding'] != 'disabled'){       
   
                   if($currency_options['rounding_increment'] > 1){
   
                       $price  = $price / $currency_options['rounding_increment'];    
   
                   }   
   
                   switch($currency_options['rounding']){
   
                       case 'up':   
   
                           $rounded_price = ceil($price);
   
                           break;
   
                       case 'down':
   
                           $rounded_price = floor($price);
   
                           break;
   
                       case 'nearest':
   
                           if(version_compare(PHP_VERSION, '5.3.0') >= 0){
   
                               $rounded_price = round($price, 0, PHP_ROUND_HALF_UP);    
   
                           }else{
   
                               if($price - floor($price) < 0.5){
   
                                   $rounded_price = floor($price);        
   
                               }else{
   
                                   $rounded_price = ceil($price);                                
   
                               }
   
                           }
   
                           break;
   
                   }
   
                   if($rounded_price > 0){
   
                       $price = $rounded_price;
   
                   }
   
                   if($currency_options['rounding_increment'] > 1){
   
                       $price  = $price * $currency_options['rounding_increment'];    
   
                   }   
   
               }
   
               if($currency_options['auto_subtract'] && $currency_options['auto_subtract'] < $price){
   
                   $price = $price - $currency_options['auto_subtract'];
   
               }
   
               return $price;
   
           }
       ```
   
 *  [Bernat Torras (a11n)](https://wordpress.org/support/users/bernattorras/)
 * (@bernattorras)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/problems-with-function-apply_rounding_rules/#post-5468618)
 * Hi Gariest,
 * Thanks for your suggestion! We’ll consider it! 🙂

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Problems with function apply_rounding_rules()’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce-multilingual/assets/icon-256x256.png?rev=2937122)
 * [WPML Multilingual & Multicurrency for WooCommerce](https://wordpress.org/plugins/woocommerce-multilingual/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce-multilingual/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce-multilingual/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce-multilingual/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce-multilingual/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce-multilingual/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [Bernat Torras (a11n)](https://wordpress.org/support/users/bernattorras/)
 * Last activity: [11 years, 6 months ago](https://wordpress.org/support/topic/problems-with-function-apply_rounding_rules/#post-5468618)
 * Status: resolved