shelledroot Posted July 10, 2017 Share Posted July 10, 2017 (edited) For the project I'm working on I am trying to display the GP on page of a mule but I can't get it to correctly show the notation. :x $holdingAmount = $Mulesrow['holdingamount']; if($holdingAmount >= 1000){ $holdingLength = strlen($holdingAmount); $strArg = 1; for($i = 0; $i < $holdingLength; $i++){ $strArg = $strArg * 10; } if ($holdingAmount >= 1000000) { $holdingAmount = floor(($holdingAmount / 1000000) * $strArg ) / $strArg . "M"; } else if ($holdingAmount >= 1000) { $holdingAmount = floor(($holdingAmount / 1000) * 100) / 100 . "K"; } } I need it to show the correct amount depending on the annotation. So if I for example have 21119999 it should show 21.11M instead of 21.12M. This should be done so depending on the length. Edited July 10, 2017 by shelledroot Quote Link to comment Share on other sites More sharing options...
Explv Posted July 10, 2017 Share Posted July 10, 2017 (edited) There are three different rounding functions, they are often found in all languages: floor (round down) round (round up or down) ceil (round up) Use floor instead of round. You need some extra logic if you want to perform the operation to n decimal places: floor(value * (10 ^ precision)) / (10 ^ precision) So in this case, as you want it to 2dp it would be: floor(value * 100) / 100 $holdingAmount = 21119054; if ($holdingAmount >= 1000000) { echo (floor(($holdingAmount / 1000000) * 100) / 100) . "m"; } else if ($holdingAmount >= 1000) { echo (floor(($holdingAmount / 1000) * 100) / 100) . "k"; } else { echo $holdingAmount; } I would also strongly recommend that you improve your "Googling" ability. All you had to do was type "php round down to 2 decimal places". Edited July 10, 2017 by Explv Quote Link to comment Share on other sites More sharing options...
shelledroot Posted July 10, 2017 Author Share Posted July 10, 2017 (edited) 28 minutes ago, Explv said: There are three different rounding functions, they are often found in all languages: floor (round down) round (round up or down) ceil (round up) Use floor instead of round. You need some extra logic if you want to perform the operation to n decimal places: floor(value * (10 ^ precision)) / (10 ^ precision) So in this case, as you want it to 2dp it would be: floor(value * 100) / 100 $holdingAmount = 21119054; if ($holdingAmount >= 1000000) { echo (floor(($holdingAmount / 1000000) * 100) / 100) . "m"; } else if ($holdingAmount >= 1000) { echo (floor(($holdingAmount / 1000) * 100) / 100) . "k"; } else { echo $holdingAmount; } I would also strongly recommend that you improve your "Googling" ability. All you had to do was type "php round down to 2 decimal places". Sorry I formed my answer poorly. I'll update it and also rephrase it here: I want the number notation like RS so 21.11m but no fallover which will be generated due to 21.11999999999. I need the notation to stay true at all times. Not just depending on the N decimal place but a variable N length. Edited July 10, 2017 by shelledroot Quote Link to comment Share on other sites More sharing options...
Explv Posted July 10, 2017 Share Posted July 10, 2017 (edited) 3 minutes ago, shelledroot said: Sorry I formed my answer poorly. I'll update it and also rephrase it here: I want the number notation like RS so 21.11m but no fallover which will be generated due to 21.11999999999. I need the notation to stay true at all times. Not just depending on the N decimal place. The code I just showed you will format it to 21.11m, it will always round down to 2dp... I have no idea what you are asking now... Edited July 10, 2017 by Explv 1 Quote Link to comment Share on other sites More sharing options...
shelledroot Posted July 10, 2017 Author Share Posted July 10, 2017 (edited) 1 hour ago, Explv said: The code I just showed you will format it to 21.11m, it will always round down to 2dp... I have no idea what you are asking now... $holdingAmount = $Mulesrow['holdingamount']; if($holdingAmount >= 1000){ $holdingLength = strlen($holdingAmount); switch ($holdingLength) { case 4: $dotPlacementHolding = 1; break; case 5: $dotPlacementHolding = 2; break; case 6: $dotPlacementHolding = 3; break; case 7: $dotPlacementHolding = 1; break; case 8: $dotPlacementHolding = 2; break; case 9: $dotPlacementHolding = 3; break; case 10: $dotPlacementHolding = 4; break; default: break; } if ($holdingAmount >= 1000000) { $holdingAmount = substr_replace($holdingAmount, ".", $dotPlacementHolding, 0); $holdingAmount = substr($holdingAmount, 0, -4) . "M"; } else if ($holdingAmount >= 1000) { $holdingAmount = substr_replace($holdingAmount, ".", $dotPlacementHolding, 0); $holdingAmount = substr($holdingAmount, 0, -1) . "K"; } } I sloppy fixed something together. My initial question was wrong, though you answered that question correctly. Blame that on not enough sleep. I shall sleep and fix it properly in the morning. What I meant to ask is I want to do proper RS notation How would I do that without rounding the number up or down at a certain N decimal. This fix works for now until i revisit it but I thought there would be a nicer way to do it. Edited July 10, 2017 by shelledroot Quote Link to comment Share on other sites More sharing options...
Explv Posted July 10, 2017 Share Posted July 10, 2017 (edited) 2 minutes ago, shelledroot said: $holdingAmount = $Mulesrow['holdingamount']; if($holdingAmount >= 1000){ $holdingLength = strlen($holdingAmount); switch ($holdingLength) { case 4: $dotPlacementHolding = 1; break; case 5: $dotPlacementHolding = 2; break; case 6: $dotPlacementHolding = 3; break; case 7: $dotPlacementHolding = 1; break; case 8: $dotPlacementHolding = 2; break; case 9: $dotPlacementHolding = 3; break; case 10: $dotPlacementHolding = 4; break; default: break; } if ($holdingAmount >= 1000000) { $holdingAmount = substr_replace($holdingAmount, ".", $dotPlacementHolding, 0); $holdingAmount = substr($holdingAmount, 0, -4) . "M"; } else if ($holdingAmount >= 1000) { $holdingAmount = substr_replace($holdingAmount, ".", $dotPlacementHolding, 0); $holdingAmount = substr($holdingAmount, 0, -1) . "K"; } } wtf are you doing Are you trying to say that you don't want any rounding to occur at all? Edited July 10, 2017 by Explv Quote Link to comment Share on other sites More sharing options...
HeyImJamie Posted July 10, 2017 Share Posted July 10, 2017 https://stackoverflow.com/questions/12277945/php-how-do-i-round-down-to-two-decimal-places Wowsohard Quote Link to comment Share on other sites More sharing options...
shelledroot Posted July 10, 2017 Author Share Posted July 10, 2017 4 minutes ago, Explv said: wtf are you doing Are you trying to say that you don't want any rounding to occur at all? Yeah I guess so. Never mind about this. :\ I'm reading an int from a DB table and want to save space on the table row I'm displaying. But I want it to correctly show. if I just round it at 2 decimals the 3rd decimal flows over still and so on and so forth. Basically I wanted the RS notation in my table, that code gives me that although it isn't nice looking. :\ I was thinking since there is an pattern there should be a recursive solution to this problem but my mind is in need of sleep. Quote Link to comment Share on other sites More sharing options...