Jump to content

Recommended Posts

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;

 

 

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.

<?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);
?>

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();
?>

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.

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.

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

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.