eleos Posted November 27, 2011 Share Posted November 27, 2011 Hello all it is my 1st post so welcome me i need to make php check if the number is 3 decimals and the last digit is 0. if it is 0 it will echo something if it is not 0 then it will echo something else i came up with this code but i dont know how to make it check if the decimal has a trail of 0 (zero) <?php if(strpos($Price,".090") !== false){ echo "there is 0 at the end of the price"; //here code to remove the 0 if it is possible }else{ echo "there is no 0 at the end of the price"; // echo the normal price } ?> this will work correctly for the price $4.090 , 12.090 etc but how can i do it in php so it will check if there is 0 at the end of the decimal? Quote Link to comment https://forums.phpfreaks.com/topic/251915-one-very-simple-question-about-php-and-decimals/ Share on other sites More sharing options...
ManiacDan Posted November 28, 2011 Share Posted November 28, 2011 substr() takes negative arguments: substr($Price, -1, 1); //gives '0' Quote Link to comment https://forums.phpfreaks.com/topic/251915-one-very-simple-question-about-php-and-decimals/#findComment-1291682 Share on other sites More sharing options...
eleos Posted November 28, 2011 Author Share Posted November 28, 2011 Thanks for the reply Maniac, How can i add your suggestion to my code? Actually what i need is a code that will search if there is 3 digit var and if this has 0 at the end it will remove it. (the xx thing is what i need) <?php if(strpos($this->price($_product),".xx0") !== false){ echo "there is 0 at the end of the price"; //here code to remove the 0 if it is possible }else{ echo "there is no 0 at the end of the price"; // echo the normal price } ?> the strange think is that for 0.120 it will work correctly with (".0)" but with 0.01 it will add the zero (0.010) Thank you Quote Link to comment https://forums.phpfreaks.com/topic/251915-one-very-simple-question-about-php-and-decimals/#findComment-1291705 Share on other sites More sharing options...
eleos Posted November 28, 2011 Author Share Posted November 28, 2011 please someone help. to make it more simple i need to add more search in the : <?php if(strpos($this->price($_product),".xx0") !== false){ echo i need it to be like this: <?php if(strpos($this->price($_product),".0",".01",".02",".03",".04",".05",".06",".07",".08",".09") !== false){ echo if i use the .0 only i get 90% of what i need but when price is 0.01 it will show 0.100 so i need to tricky add ".01" as well please someone! Quote Link to comment https://forums.phpfreaks.com/topic/251915-one-very-simple-question-about-php-and-decimals/#findComment-1291716 Share on other sites More sharing options...
ManiacDan Posted November 28, 2011 Share Posted November 28, 2011 First you have to see if you're dealing with strings or numbers. The STRING "1.010" will have a zero at the end, the NUMBER 1.010 will not. I'm still not clear what you mean, first you say you want to detect it, then you say you want to remove it, and then you're saying that the problem is for one example it's being removed. What is the actual problem. Show some examples of "before" data and what you want to happen for each case. There's probably a pure math implementation for this, but you could also use regular expressions. Don't bump your thread after 15 minutes in the middle of the night on a sunday, you won't get any more help if you keep doing that Quote Link to comment https://forums.phpfreaks.com/topic/251915-one-very-simple-question-about-php-and-decimals/#findComment-1291718 Share on other sites More sharing options...
eleos Posted November 28, 2011 Author Share Posted November 28, 2011 Sorry for the bump, i am just sure that it must be something simple but i am not advance in php and it is driving me crazy! Let me try to explain: I have an eshop that i want to use 3 decimal for the price display because i sell products even less than 1 cent (0.150) etc. 0.090 it is ok , it helps the user understand that the price is less than 1 cent but if i want to sell something 10 cents i will need to add it like : 0.10 Problem is that in this example magento will add the 3rd decimal 0 and the price will look 0.100 this (after my code) happens only between the price 0.01 - 0.09 the code that works very good is this: <?php if(strpos($this->price($_product),".0") !== false){ echo number_format($_product->getPrice(),3,',','.'); }else{ echo number_format($_product->getPrice(),2,',','.'); } ?> I would need to modify the strpos function to check for multiple things in the decimal price that gets printed and then echo the result Normaly with the ".0" it should work even for the 0.10 but for some way it will not. Is it possible to modify the first part of the code so it can be like my example? (".0" + ".01-09") Thank you Quote Link to comment https://forums.phpfreaks.com/topic/251915-one-very-simple-question-about-php-and-decimals/#findComment-1291758 Share on other sites More sharing options...
ManiacDan Posted November 28, 2011 Share Posted November 28, 2011 0.09 is not less than one cent. Are you saying that you're displaying these prices: 0.090 -- "Point zero nine cents" -- $0.0009 0.10 -- "ten cents" -- $0.10 if that's the case, you should just always display in dollars and not run into this confusion at all. I still don't have before and after data, last time I'll ask for it. Quote Link to comment https://forums.phpfreaks.com/topic/251915-one-very-simple-question-about-php-and-decimals/#findComment-1291854 Share on other sites More sharing options...
eleos Posted November 28, 2011 Author Share Posted November 28, 2011 Dear ManiacDan, i have only 3 decimals for all the prices (not 4) but in 9 cases i need the decimal to be 2 more advanced. this cases are from 1 - 9 cents , and they should be 0.01 - 0.09 but because of the 3 decimal rule magento will convert this prices to 0.090. If i need to display 10 cents it is not problem and with my code it will be displayed 0.10 (not 0.100) So i cannot understand why only for this 9 numbers it doesnt remove the trail zero. My thought was maybe to trick it so the (.0) will be still there to control all the other prices but as well adding multiple the other 9 numbers to search (.01)(.02)(.03)(.04)(.05)(0.6)(0.7)(0.(0.9) I Pasted all the file that controls among other things, the echo price as well, hope it will help. Thank you very much for helping pastbin: http://pastebin.com/FSfcDyer Quote Link to comment https://forums.phpfreaks.com/topic/251915-one-very-simple-question-about-php-and-decimals/#findComment-1291927 Share on other sites More sharing options...
Andy-H Posted November 28, 2011 Share Posted November 28, 2011 $dec = explode('.', $this->price($_product)); $dec = array_pop($dec); if ( preg_match('/[0-9]{2}0/', $dec) ) { return number_format($this->price($_product), 2); } // or with Dan's code if ( strlen($dec) == 3 && substr($dec, -1) == 0 ) { return number_format($this->price($_product), 2); } Quote Link to comment https://forums.phpfreaks.com/topic/251915-one-very-simple-question-about-php-and-decimals/#findComment-1291934 Share on other sites More sharing options...
Psycho Posted November 28, 2011 Share Posted November 28, 2011 I think you are over-complicating this. I think I understand that you have some prices that can be fractions of a cent. But, it is not clear as ManiacDan has stated previously because your examples don't make sense. You state that .15 is less than one cent, but I interpret that as 15 cents. I agree with him that you shoudl display everything in dollars. So, $0.015 would represent 1.5 cents. So, in order to determine if you need two decimals or three just check of the value rounded to 2 decimals is equal to the original value. This seems to work for both strings and numeric values: function customFormat($amount) { $decimals = ($amount==round($amount, 2)) ? 2 : 3; return number_format($amount, $decimals); } //Unit Test $numbers = array(1.015, 1.1, 1.00, 1.10, 2, .005, '1.015', '1.1', '1.100', '1.10', '2', '.005'); foreach($numbers as $number) { echo "{$number} : " . customFormat($number) . "<br>\n"; } Results Orig. : Formated 1.015 : 1.015 1.1 : 1.10 1 : 1.00 1.1 : 1.10 2 : 2.00 0.005 : 0.005 1.015 : 1.015 1.1 : 1.10 1.100 : 1.10 1.10 : 1.10 2 : 2.00 .005 : 0.005 Quote Link to comment https://forums.phpfreaks.com/topic/251915-one-very-simple-question-about-php-and-decimals/#findComment-1291961 Share on other sites More sharing options...
ManiacDan Posted November 28, 2011 Share Posted November 28, 2011 mjdamato has provided what I've asked you for twice now. If this is not what you want, take mjdamato's table, alter the output appropriately, and re-post it. Quote Link to comment https://forums.phpfreaks.com/topic/251915-one-very-simple-question-about-php-and-decimals/#findComment-1291967 Share on other sites More sharing options...
eleos Posted November 28, 2011 Author Share Posted November 28, 2011 I think yes this is exactly what i will need! but how i will add this code to echo the number of the price? basically the code that displays the price is this : <?php echo $this->getPriceHtml($_product, true) ?> or even <?php echo $this->getPriceHtml($_product) ?> if i tag all your code in php is it ok? <?php function customFormat($amount) { $decimals = ($amount==round($amount, 2)) ? 2 : 3; return number_format($amount, $decimals); } //Unit Test $numbers = array(1.015, 1.1, 1.00, 1.10, 2, .005, '1.015', '1.1', '1.100', '1.10', '2', '.005'); foreach($numbers as $number) { echo "{$number} : " . customFormat($number) . "<br>\n"; } ?> or they need to go seperately the function and the echo? thank you so much, i will try find some light and i will re-post! EDIT: OK, they need to go seperetaly. The function needs to be first and the array inside the loop of the products. I will try to make some sense now to echo $this->getPriceHtml($_product) Quote Link to comment https://forums.phpfreaks.com/topic/251915-one-very-simple-question-about-php-and-decimals/#findComment-1291970 Share on other sites More sharing options...
ManiacDan Posted November 28, 2011 Share Posted November 28, 2011 The getPriceHtml() function needs to be altered, unless it's a magical function that doesn't actually exist. If it's magical, make a new function with a custom name that wraps getPriceHtml() and formats it properly, so you don't have any weird code in your display logic. Quote Link to comment https://forums.phpfreaks.com/topic/251915-one-very-simple-question-about-php-and-decimals/#findComment-1291981 Share on other sites More sharing options...
eleos Posted November 28, 2011 Author Share Posted November 28, 2011 there is allready a function for getpricehtml, in 2 files: * Returns product price block html * * @param Mage_Catalog_Model_Product $product * @param boolean $displayMinimalPrice * @param string $idSuffix * @return string */ public function getPriceHtml($product, $displayMinimalPrice = false, $idSuffix = '') { $type_id = $product->getTypeId(); if (Mage::helper('catalog')->canApplyMsrp($product)) { $realPriceHtml = $this->_preparePriceRenderer($type_id) ->setProduct($product) ->setDisplayMinimalPrice($displayMinimalPrice) ->setIdSuffix($idSuffix) ->toHtml(); $product->setAddToCartUrl($this->getAddToCartUrl($product)); $product->setRealPriceHtml($realPriceHtml); $type_id = $this->_mapRenderer; } return $this->_preparePriceRenderer($type_id) ->setProduct($product) ->setDisplayMinimalPrice($displayMinimalPrice) ->setIdSuffix($idSuffix) ->toHtml(); } and this small in another file. public function getPriceHtml($product) { $this->setTemplate('catalog/product/price.phtml'); $this->setProduct($product); return $this->toHtml(); } } It will be really advanced for me I really enjoy learning php but this is difficult :'( I would kindly ask you to help me a little more on this cause i see no light what to do now. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/251915-one-very-simple-question-about-php-and-decimals/#findComment-1291991 Share on other sites More sharing options...
eleos Posted November 28, 2011 Author Share Posted November 28, 2011 Let's say that i leave the function loaded as it is, Will i have to do something like ?php //Unit Test $numbers = strpos($this->getPriceHtml($_product); foreach($numbers as $number) { echo "{$number} : " . customFormat($number) . "<br>\n"; } ?> Or rewrite the function with a mix from the original $getpricehtml function and the $amount function? the array with the numbers was just an example to query some numbers, correctly? headache Quote Link to comment https://forums.phpfreaks.com/topic/251915-one-very-simple-question-about-php-and-decimals/#findComment-1292001 Share on other sites More sharing options...
Psycho Posted November 28, 2011 Share Posted November 28, 2011 I don't know why you would have two different functions with the same name. Perhaps one isn't even used? You could either: 1. Use the function I provided and call it wherever you are getting the price. echo customFormat($this->getPriceHtml($_product)); 2. Or you could modify the getPriceHtml() functions to implement the same logic But, there is a lot going on in those functions that I don't have any knowledge of and I don't have the time or the inclination to deconstruct the magento framework to figure out how to implement it in the function without breaking something else. Quote Link to comment https://forums.phpfreaks.com/topic/251915-one-very-simple-question-about-php-and-decimals/#findComment-1292017 Share on other sites More sharing options...
eleos Posted December 11, 2011 Author Share Posted December 11, 2011 Thanks for all the replys , i was able to fix this with a mix of your suggestions and some further research. However i have a price that comes from this string and i want it allways display 2 decimals. <?php echo Mage::helper('checkout')->formatPrice($this->getSubtotal()) ?> i tried change the line and add tofixed and other methods (2) but i am not really good in php. How this could be done to force it allways return 2 decimals? i tried for example something like this but it doesnt work: <?php echo number_format Mage::helper('checkout')->formatPrice($this->getSubtotal()),2 ?> Quote Link to comment https://forums.phpfreaks.com/topic/251915-one-very-simple-question-about-php-and-decimals/#findComment-1296659 Share on other sites More sharing options...
ManiacDan Posted December 11, 2011 Share Posted December 11, 2011 number_format() is a function. Functions require that you surround their arguments in parentheses. Also, "doesn't work" in this case is a very explicit error message. Either you don't have errors turned on or you're not telling us what they are. Both would be mistakes. <?php echo number_format( Mage::helper('checkout')->formatPrice($this->getSubtotal()), 2 ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/251915-one-very-simple-question-about-php-and-decimals/#findComment-1296665 Share on other sites More sharing options...
eleos Posted December 11, 2011 Author Share Posted December 11, 2011 this code gives this error. Warning: number_format() expects parameter 1 to be double, string given in /Volumes/.. .. maybe it would be more easy to force this to return 3 decimals? <?php echo ($_price['formated_price']) ?> as long as i try to add number_format in this ,it will give the same error. i tried something like this but with no luck: <?php echo number_format($_price['formated_price']),3 ?> Quote Link to comment https://forums.phpfreaks.com/topic/251915-one-very-simple-question-about-php-and-decimals/#findComment-1296670 Share on other sites More sharing options...
ManiacDan Posted December 11, 2011 Share Posted December 11, 2011 Mage::helper('checkout')->formatPrice($this->getSubtotal()) That function returns a formatted string. You cannot re-format a formatted string. You'll have to add the decimal place definition to the formatPrice() function. I bet it already has one, actually. Quote Link to comment https://forums.phpfreaks.com/topic/251915-one-very-simple-question-about-php-and-decimals/#findComment-1296868 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.