Jump to content

Number Formatting, No Rounding


The Letter E

Recommended Posts

Hey Everybody,

 

I was replying to someone elses question about number formatting and came up with a class that will format a number of any length with decimal or not, without rounding. It works perfectly, but after a comment by a noteworthy member my curiosity was aroused.

 

Is there an easier/faster/better way to achieve this??

 

 

My Object:

<?php

/*GHETTO NUMBER FORMATTER v1.0*/

class ghettoNumber {

	private $input;
	private $output;

	public $whole;
	public $decimal;
	public $number;

	function __construct($input){
		$this->input = $input;
		$this->output = $output;
		$this->number = $number;
		$this->whole = $whole;
		$this->decimal = $decimal;
	}

	private function ghettoFormat(){

		//Split number at the .(decimal)
		$this->number = explode('.', $this->input);
		//Define the whole number
		$this->whole = $this->number[0];
		//Format the whole number
		$this->whole = number_format($this->whole, 0,'', ',');
		//Define the decimal
		$this->decimal = $this->number[1];
		//Format the decimal
		$this->decimal = rtrim($this->decimal, '0');

		if(is_string($this->input)){
			$this->output = $this->decimal != '' ? $this->whole.'.'.$this->decimal : $this->whole;
		}
		else{
			$this->output = '<strong>ERROR:</strong> Input arg must be passed as type: <em>string</em>';
		}

		//Return result
		return 'Original - '.$this->input.'<br>
					  GhettoFormat - '.$this->output.'
						<br><br>';

	}

	public function makeItGhetto(){
		return $this->ghettoFormat();
	}
}

?>

 

Sample Instantiation:

$ghettoFormat = new ghettoNumber('12565456565.123401201001210000');
                print $ghettoFormat->makeItGhetto();

 

 

I will reiterate my question once more...

Is there, in your opinion, a BETTER way to achieve this (number formatting w/o rounding)?

 

Thanks People,

 

E

Link to comment
Share on other sites

what can I use this for that I can't do with the number_format function?

http://php.net/manual/en/function.number-format.php

 

That's where the meat of my question lies, I have not been able to achieve a 'non-rounded' number using number_format(), is there an arg to that I am not using??

 

Thus far I have not been able to achieve this format without the number being rounded.

Link to comment
Share on other sites

what can I use this for that I can't do with the number_format function?

http://php.net/manual/en/function.number-format.php

 

That's where the meat of my question lies, I have not been able to achieve a 'non-rounded' number using number_format(), is there an arg to that I am not using??

 

Thus far I have not been able to achieve this format without the number being rounded.

A number doesn't need to have a decimal, but if they do, they can either have non-infinite and infinite number of decimals... When it's infinite number of decimals you must in the end round to get closest to the actual number, not rounding would be wrong...

 

anyways, if the number has freaking many digits:

http://www.phpfreaks.com/forums/php-coding-help/make-a-number-readable/msg1499622/#msg1499622

 

 

anyways, give us an example of what you want to do

Link to comment
Share on other sites

what can I use this for that I can't do with the number_format function?

http://php.net/manual/en/function.number-format.php

 

That's where the meat of my question lies, I have not been able to achieve a 'non-rounded' number using number_format(), is there an arg to that I am not using??

 

Thus far I have not been able to achieve this format without the number being rounded.

A number doesn't need to have a decimal, but if they do, they can either have non-infinite and infinite number of decimals... When it's infinite number of decimals you must in the end round to get closest to the actual number, not rounding would be wrong...

 

anyways, if the number has freaking many digits:

http://www.phpfreaks.com/forums/php-coding-help/make-a-number-readable/msg1499622/#msg1499622

 

 

anyways, give us an example of what you want to do

 

The example is exactly what my object already does, just wondering if there is a more efficient way to achieve the same effect.

 

Example:

 

Number: 11554516516.215015651156510151001561000

Formatted: 11,554,516,516.215015651156510151001561

 

Number: 1234.5056000

Formatted: 1,234.5056

 

Number: 12.50

Formatted: 12.5

 

Number: 12345.000

Formatted: 12,345

 

 

As you can see, it keeps the number EXACTLY the same as the input. It just adds commas for thousands delimiters and removes 0's from the end of the decimal, does not round or change the value of the number in any way.

 

As far as I can tell you cannot achieve this same effect using only number_format(), am I wrong?

Link to comment
Share on other sites

if I understand what you mean correctly, then you could just use preg_replace...

or am I wrong then?

 

brb

 

I don't feel like this thread is summed up as easily as right and wrong. Yes, you're right, preg_replace would be one possible function that could be used to do a work around for the same effect.

 

I'm simply looking for the MOST EFFICIENT way to do this. If there is a way to do it using only number_format() I would definitely defer to that.

Link to comment
Share on other sites

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.