jason97673 Posted April 27, 2009 Share Posted April 27, 2009 Ok this should be a rather simple question. Im inserting data into a database with the number format being 0.6278(Sample number). Using PHP, how do I get this number to turn into 62.78? Should be simple, but unsure exactly how to do it as im a beginner. Quote Link to comment https://forums.phpfreaks.com/topic/155846-switch-decimal-place-using-decimal-numbers/ Share on other sites More sharing options...
Mchl Posted April 27, 2009 Share Posted April 27, 2009 http://en.wikipedia.org/wiki/Multiplication * 100 Quote Link to comment https://forums.phpfreaks.com/topic/155846-switch-decimal-place-using-decimal-numbers/#findComment-820301 Share on other sites More sharing options...
.josh Posted April 27, 2009 Share Posted April 27, 2009 php does not support multiplication before version 6. It also features other advanced things like addition and subtraction. They are still working on division; since dividing by zero is impossible, the developers are working on a way for php to fall back on an error message instead of completely crashing. They do not expect for php to be capable of division operations until at least version 7. php6 is relatively new; most people are still using some version of 4 or 5. So unless you've downloaded and installed php6, You will have to do it by other means. You can, for instance, do this (requires php5. If you're still on 4, then I suggest upgrading...): $place = 3; // if you have a leading 0, must make this 1 more to account for it! $string = "0.6278"; $string = str_replace('.','',$string); $string = str_split($string); foreach ($string as $pos => $n) { $newString .= ($pos == $place)? "." . $n : $n; } $newString = (float) $newString; echo $newString; Quote Link to comment https://forums.phpfreaks.com/topic/155846-switch-decimal-place-using-decimal-numbers/#findComment-820336 Share on other sites More sharing options...
Mchl Posted April 27, 2009 Share Posted April 27, 2009 Ah... sorry... I forgot I installed 6.0alpha on my development machine. Quote Link to comment https://forums.phpfreaks.com/topic/155846-switch-decimal-place-using-decimal-numbers/#findComment-820337 Share on other sites More sharing options...
premiso Posted April 27, 2009 Share Posted April 27, 2009 Ah... sorry... I forgot I installed 6.0alpha on my development machine. You should be jerk. It took us a long time to work out how to do this and you just over "simplify" it because you are testing a new version... :'( You should be ashamed of yourself. Quote Link to comment https://forums.phpfreaks.com/topic/155846-switch-decimal-place-using-decimal-numbers/#findComment-820358 Share on other sites More sharing options...
sasa Posted April 27, 2009 Share Posted April 27, 2009 <?php function move_dot($number, $places) { $pices = split('\.', $number); if(count($pices) == 1) $pices[1]=''; if ($places > 0){ if (strlen($pices[1]) > $places){ $pices[0] .= substr($pices[1],0, $places); $pices[1] = substr($pices[1], $places); $pices[0] = $pices[0] + 0; return $pices[0].'.'.$pices[1]; } else { return $pices[0].$pices[1].str_repeat('0', $places - strlen($pices[1])); } } if ($places == 0) return $number; if (strlen($pices[0]) + $places > 0){ $pices[1] = substr($pices[0], $places).$pices[1]; $pices[0] = substr($pices[0], 0, strlen($pices[0]) + $places); return $pices[0].'.'.$pices[1]; } return '0.'.str_repeat('0', -$places - strlen($pices[0])).$pices[0].$pices[1]; } echo move_dot(0.1234, 2); ?> Quote Link to comment https://forums.phpfreaks.com/topic/155846-switch-decimal-place-using-decimal-numbers/#findComment-820488 Share on other sites More sharing options...
.josh Posted April 27, 2009 Share Posted April 27, 2009 see you're using addition and subtraction though. That will only work in php6+ and if he has that, he could just multiply by 100 in the first place. Quote Link to comment https://forums.phpfreaks.com/topic/155846-switch-decimal-place-using-decimal-numbers/#findComment-820523 Share on other sites More sharing options...
genericnumber1 Posted April 27, 2009 Share Posted April 27, 2009 Obviously the best solution is object oriented with method chaining for more readable code. <?php class UncommittedOrderException extends Exception {} class NonexistantOrderException extends Exception {} class IncompleteOrderException extends Exception {} class EmptyOrderListException extends Exception {} class UnsetNumberException extends Exception {} class UnsetShiftedCharacterException extends Exception {} class DirectionOrder { private $direction = null; private $distance = null; private $callingShifter; public function __construct(NumberShifter $callingShifter) { $this->callingShifter = $callingShifter; } public function setDirection($direction) { $this->direction = $direction; return $this; } public function setDistance($distance) { $this->distance = $distance; return $this; } public function commitOrder() { if($this->direction === null || $this->distance === null) { throw new IncompleteOrderException(); } $this->callingShifter->commitOrder(); return $this->callingShifter; } public function getDirection() { return $this->direction; } public function getDistance() { return $this->distance; } } class NumberShifter { const LEFT = 1; const RIGHT = 2; private $orders = array(); private $order = null; private $number = null; private $character = null; public function setNumber($number) { $this->number = $number; return $this; } public function getNumberAsFloat() { return (float) $this->number; } public function setShiftedCharacter($character) { $this->character = $character; return $this; } public function newDirectionOrder() { if($this->order !== null) throw new UncommittedOrderException(); $this->order = new DirectionOrder($this); return $this->order; } public function commitOrder() { if($this->order === null) throw new NonexistantOrderException(); $this->orders[] = $this->order; $this->order = null; } public function shift() { if($this->order !== null) throw new UncommittedOrderException(); if(count($this->orders) == 0) throw new EmptyOrderListException(); if($this->number === null) throw new UnsetNumberException(); if($this->character === null) throw new UnsetShiftedCharacterException(); $pieces = explode($this->character, $this->number); foreach($pieces as &$piece) { $piece = str_split($piece); } foreach($this->orders as $order) { $end = count($pieces) - 1; for($i = 0; $i < $order->getDistance(); ++$i) { for($j = 0; $j < $end; ++$j) { if($order->getDirection() == self::RIGHT) { if(count($pieces[$j+1]) == 0) { $pieces[$j+1][] = 0; } $pieces[$j][] = array_shift($pieces[$j+1]); } else { if(count($pieces[$j]) == 0) { $pieces[$j][] = 0; } array_unshift($pieces[$j+1], array_pop($pieces[$j])); } } } } foreach($pieces as &$piece) { $piece = implode('', $piece); } $this->number = implode($this->character, $pieces); } } $shifter = new NumberShifter(); $shifter->setNumber(0.6278) ->setShiftedCharacter('.') ->newDirectionOrder() ->setDirection(NumberShifter::RIGHT) ->setDistance(2) ->commitOrder() ->shift(); echo $shifter->getNumberAsFloat(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/155846-switch-decimal-place-using-decimal-numbers/#findComment-820647 Share on other sites More sharing options...
genericnumber1 Posted April 27, 2009 Share Posted April 27, 2009 Ah, you're right, no adding... But no matter, that can be fixed with more objects! <?php class Math { static public function increment(&$number) { $number = Math::addOne($number); return $number; } static public function addOne($number) { $bit = 1; while(($number & $bit) > 0) { $bit <<= 1; } while($bit >= 1) { $number ^= $bit; $bit >>= 1; } return $number; } } class UncommittedOrderException extends Exception {} class NonexistantOrderException extends Exception {} class IncompleteOrderException extends Exception {} class EmptyOrderListException extends Exception {} class UnsetNumberException extends Exception {} class UnsetShiftedCharacterException extends Exception {} class DirectionOrder { private $direction = null; private $distance = null; private $callingShifter; public function __construct(NumberShifter $callingShifter) { $this->callingShifter = $callingShifter; } public function setDirection($direction) { $this->direction = $direction; return $this; } public function setDistance($distance) { $this->distance = $distance; return $this; } public function commitOrder() { if($this->direction === null || $this->distance === null) { throw new IncompleteOrderException(); } $this->callingShifter->commitOrder(); return $this->callingShifter; } public function getDirection() { return $this->direction; } public function getDistance() { return $this->distance; } } class NumberShifter { const LEFT = 1; const RIGHT = 2; private $orders = array(); private $order = null; private $number = null; private $character = null; public function setNumber($number) { $this->number = $number; return $this; } public function getNumberAsFloat() { return (float) $this->number; } public function setShiftedCharacter($character) { $this->character = $character; return $this; } public function newDirectionOrder() { if($this->order !== null) throw new UncommittedOrderException(); $this->order = new DirectionOrder($this); return $this->order; } public function commitOrder() { if($this->order === null) throw new NonexistantOrderException(); $this->orders[] = $this->order; $this->order = null; } public function shift() { if($this->order !== null) throw new UncommittedOrderException(); if(count($this->orders) == 0) throw new EmptyOrderListException(); if($this->number === null) throw new UnsetNumberException(); if($this->character === null) throw new UnsetShiftedCharacterException(); $pieces = explode($this->character, $this->number); foreach($pieces as &$piece) { $piece = str_split($piece); } foreach($this->orders as $order) { $end = count($pieces) - 1; for($i = 0; $i < $order->getDistance(); Math::increment($i)) { for($j = 0; $j < $end; Math::increment($j)) { if($order->getDirection() == self::RIGHT) { if(count($pieces[Math::addOne($j)]) == 0) { $pieces[Math::addOne($j)][] = 0; } $pieces[$j][] = array_shift($pieces[Math::addOne($j)]); } else { if(count($pieces[$j]) == 0) { $pieces[$j][] = 0; } array_unshift($pieces[Math::addOne($j)], array_pop($pieces[$j])); } } } } foreach($pieces as &$piece) { $piece = implode('', $piece); } $this->number = implode('.', $pieces); } } $shifter = new NumberShifter(); $shifter->setNumber(0.6278) ->setShiftedCharacter('.') ->newDirectionOrder() ->setDirection(NumberShifter::RIGHT) ->setDistance(2) ->commitOrder() ->shift(); echo $shifter->getNumberAsFloat(); ?> fixed. Quote Link to comment https://forums.phpfreaks.com/topic/155846-switch-decimal-place-using-decimal-numbers/#findComment-820677 Share on other sites More sharing options...
DarkWater Posted April 27, 2009 Share Posted April 27, 2009 This is fabulous. Quote Link to comment https://forums.phpfreaks.com/topic/155846-switch-decimal-place-using-decimal-numbers/#findComment-820733 Share on other sites More sharing options...
gffg4574fghsDSGDGKJYM Posted April 27, 2009 Share Posted April 27, 2009 This is fabulous. It's ugly these so called php developer don't care about accessibility or internationalisation , how dare they don't even use any XML to solve a problem like this. The code provide doesn't have a chance to work properly in a live production website. It's a inelegant solution and the developer on this post should be ashamed of themself. Quote Link to comment https://forums.phpfreaks.com/topic/155846-switch-decimal-place-using-decimal-numbers/#findComment-820755 Share on other sites More sharing options...
.josh Posted April 27, 2009 Share Posted April 27, 2009 that is true! we could just outsource the math operations to something else and have the value returned! brilliant! Quote Link to comment https://forums.phpfreaks.com/topic/155846-switch-decimal-place-using-decimal-numbers/#findComment-820759 Share on other sites More sharing options...
jason97673 Posted April 28, 2009 Author Share Posted April 28, 2009 Hmm, at first I felt kind of stupid for not knowing to just multiply by 100 since its basic math but as others have posted since mine, seems like there is more too it. However, you said multiplication is only supported in version 6? Well My server has 5.2.6 and the code I entered was $cmppct = $row['cmppct'] * 100; This just retrieved a number from a database and multiplied it by 100 then it displayed perfectly in the form of xx.xx. So I dont know why its said that php versions less then 6 dont support it because it appears to have worked. But I have another simple question relating to this. Some of the data in the database might be a whole number like 68.0 but with the way I am doing the PHP most of the data has a decimal place while some do not. So I want to format the number to make sure it ALWAYS includes the decimal place such as 68.0 or 68.2 and so on instead of 68 and 68.2. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/155846-switch-decimal-place-using-decimal-numbers/#findComment-820878 Share on other sites More sharing options...
nrg_alpha Posted April 28, 2009 Share Posted April 28, 2009 So I want to format the number to make sure it ALWAYS includes the decimal place such as 68.0 or 68.2 and so on instead of 68 and 68.2. number_format Quote Link to comment https://forums.phpfreaks.com/topic/155846-switch-decimal-place-using-decimal-numbers/#findComment-820882 Share on other sites More sharing options...
jason97673 Posted April 28, 2009 Author Share Posted April 28, 2009 Yeah, I just did some research on my own and came up with this piece of code to do what I asked for $cmppct1 = number_format($cmppct, 1, '.', ''); Quote Link to comment https://forums.phpfreaks.com/topic/155846-switch-decimal-place-using-decimal-numbers/#findComment-820884 Share on other sites More sharing options...
Daniel0 Posted April 28, 2009 Share Posted April 28, 2009 Hmm, at first I felt kind of stupid for not knowing to just multiply by 100 since its basic math but as others have posted since mine, seems like there is more too it. However, you said multiplication is only supported in version 6? Well My server has 5.2.6 and the code I entered was $cmppct = $row['cmppct'] * 100; This just retrieved a number from a database and multiplied it by 100 then it displayed perfectly in the form of xx.xx. So I dont know why its said that php versions less then 6 dont support it because it appears to have worked. http://www.google.com/search?q=define%3Asarcasm Quote Link to comment https://forums.phpfreaks.com/topic/155846-switch-decimal-place-using-decimal-numbers/#findComment-820957 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.